-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2616.cpp
More file actions
26 lines (26 loc) · 702 Bytes
/
2616.cpp
File metadata and controls
26 lines (26 loc) · 702 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
bool criteria(int maxVal, vector<int>& nums, int p) {
int cnt = 0;
int n = nums.size();
for (int i = 0; i + 1 < n; ++i) {
if (nums[i + 1] - nums[i] <= maxVal) {
i++;
cnt++;
}
}
return cnt >= p;
}
int minimizeMax(vector<int>& nums, int p) {
int n = nums.size();
sort(nums.begin(), nums.end());
int left = 0;
int right = nums[n - 1];
while (left < right) {
int mid = left + (right - left) / 2;
if (criteria(mid, nums, p)) right = mid;
else left = mid + 1;
}
return left;
}
};