-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1552.cpp
More file actions
27 lines (26 loc) · 752 Bytes
/
1552.cpp
File metadata and controls
27 lines (26 loc) · 752 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(vector<int>& position, int m, int interval) {
int n = position.size();
int current = INT_MIN;
int cnt = 0;
for (int i = 0; i < n; ++i) {
if (position[i] >= current + interval) {
current = position[i];
cnt += 1;
}
}
return cnt >= m;
}
int maxDistance(vector<int>& position, int m) {
sort(position.begin(), position.end());
int left = 0;
int right = INT_MAX;
while (left < right) {
int mid = left + (right - left) / 2;
if (!criteria(position, m, mid)) right = mid;
else left = mid + 1;
}
return left - 1;
}
};