算法
困难
通过率43%
题目
给定一个长度为N的数组height
代表N个人的身高
请你选出一部分人组成方阵(即矩阵的行数与列数相同),并且每一行的人的身高差不超过2。
请问最多能选取多少人参加方阵
1≤N≤1051 \leq N \leq 10^51≤N≤105
1≤height[i]≤1091 \leq height[i] \leq 10^91≤height[i]≤109
样例
输入:[2,5,4,5]
输出:4
解释:可以组成 [[2,4],[5,5]]这样的方阵
输入:[1,4,5,6,7,7,7,10,11,12]
输出:9
解释:可以组成 [[4,5,6],[7,7,7],[10,11,12]]这样的方阵
bool checkCount(vector<int> &height,int target)
{
int start = 0;
int countCol = 0;
int curIndex = 0;while (start + target -1 < height.size())
{
if (height[start + target -1 ] - height[start] >2)
{
start++;
}
else
{
countCol++;
start = start+ target;
}
}if (countCol < target)
{
return false;
}return true;
}int MaxPeopleNumber(vector<int> &height)
{
int size = height.size();
sort(height.begin(), height.end());
int maxLen = sqrt(size);
int start = 1;
int mid = (maxLen + start) / 2;while (mid >= 1 )
{
//二分法查找,如果最大满足要求,则返回
if (checkCount(height, maxLen))
{
return maxLen*maxLen;
}
else if (checkCount(height, mid)) //向最大值逼近
{
start = mid;
maxLen--;
mid = (maxLen + start) / 2;
}
else
{
maxLen = mid;
mid = (maxLen + start) / 2;
}}
return 1;
}
void test()
{
//vector<int> height = { 2, 5, 4, 5 };//4
//vector<int> height = { 1, 4, 5, 6, 7, 10, 10, 10, 11, 12 };// //4
//vector<int> height = { 1, 4, 5, 6, 7, 7, 7, 10, 11, 12 };// //9
vector<int> height = { 3,6,3,9,1,3,4,3,4 };//4
//vector<int> height = { 1, 4, 5, 6, 7, 7, 7, 10, 11, 12 };//9
int ret = MaxPeopleNumber(height);
}