-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily3.cpp
More file actions
83 lines (70 loc) · 2.12 KB
/
Copy pathdaily3.cpp
File metadata and controls
83 lines (70 loc) · 2.12 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Solution 1
class Solution {
public:
int maxDistance(vector<int>& position, int m) {
std::sort(position.begin(), position.end());
if (m == 2) return position.back() - position.front();
auto left = int{1};
auto right = position.back() - position.front();
auto baskets = std::unordered_set<int>(position.begin(), position.end());
auto result = int{0};
while (left <= right) {
auto mid = long{left + (right - left) / 2};
auto basket_num = int{1};
auto last_pos = position.front();
for (auto i = int{1}; i < position.size(); ++i) {
if (position[i] - last_pos >= mid) {
basket_num++;
last_pos = position[i];
}
if (basket_num >= m) break;
}
if (basket_num >= m) {
left = mid + 1;
result = mid;
} else if (basket_num < m) {
right = mid - 1;
}
}
return result;
}
};
// Solution 2
class Solution {
public:
bool isAcceptable(vector<int>& position, int m, int mid) {
int ballCount = 1;
int ballPosition = position[0];
for (int i = 1; i < position.size(); i++) {
if (position[i] - ballPosition >= mid) {
ballCount++;
ballPosition = position[i];
}
}
if (ballCount >= m) return true;
return false;
}
int maxDistance(vector<int>& position, int m) {
sort(position.begin(), position.end());
int start = 1;
int end = position.back() - position.front();
int result = 0;
while (start <= end) {
int mid = start + (end - start) / 2;
if (isAcceptable(position, m, mid)) {
start = mid + 1;
result = mid;
} else {
end = mid - 1;
}
}
return result;
}
};
// Used to speed up run time
auto init = [](){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
return 'c';
}();