-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_1552_maxDistance.cc
66 lines (60 loc) · 1.16 KB
/
Problem_1552_maxDistance.cc
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <algorithm>
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
// 间距 >= x ,能够存放 >= m 个小球,返回 true
bool check(int x, vector<int> &position, int m)
{
int pre = position[0];
int cnt = 1;
for (int i = 1; i < position.size(); i++)
{
if (position[i] - pre >= x)
{
pre = position[i];
cnt++;
}
}
return cnt >= m;
}
int maxDistance(vector<int> &position, int m)
{
std::sort(position.begin(), position.end());
int left = 1;
int right = position.back() - position.front();
int ans = -1;
while (left <= right)
{
// 二分找到间距
int mid = (left + right) / 2;
if (check(mid, position, m))
{
ans = mid;
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return ans;
}
};
void test()
{
Solution s;
vector<int> p1 = {1, 2, 3, 4, 7};
vector<int> p2 = {5, 4, 3, 2, 1, 1000000000};
EXPECT_EQ_INT(3, s.maxDistance(p1, 3));
EXPECT_EQ_INT(999999999, s.maxDistance(p2, 2));
EXPECT_SUMMARY;
}
int main()
{
test();
return 0;
}