-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
33 lines (31 loc) · 861 Bytes
/
solution.cpp
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
27
28
29
30
31
32
33
#include <algorithm>
#include <vector>
class Solution {
public:
int maxDistance(std::vector<int>& position, int m) {
std::sort(position.begin(), position.end());
int min = 1, max = position.back() - position[0];
while (min < max) {
int mid = min + ((max - min + 1) >> 1);
if (Check(position, mid, m)) {
min = mid;
} else {
max = mid - 1;
}
}
return min;
}
private:
bool Check(const std::vector<int>& position, int force, int m) {
int l_val = position[0];
--m;
for (size_t r = 1; r < position.size(); ++r) {
int diff = position[r] - l_val;
if (diff >= force) {
--m;
l_val = position[r];
}
}
return m <= 0;
}
};