Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b8c2486

Browse files
Time: 23 ms (77.85%), Space: 86.8 MB (50.22%) - LeetHub
1 parent e83aa9b commit b8c2486

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int minimizeMax(vector<int>& v, int p) {
4+
sort(v.begin(), v.end());
5+
int n = v.size();
6+
int low = 0, high = 1e9;
7+
8+
auto ok = [&](int x) {
9+
int cnt = 0;
10+
for (int i = 1; i < n; i++) {
11+
if (v[i] - v[i - 1] <= x) {
12+
cnt++;
13+
i++;
14+
}
15+
}
16+
return cnt >= p;
17+
};
18+
19+
int ans = 0;
20+
while (low <= high) {
21+
int mid = low + (high - low) / 2;
22+
if (ok(mid)) {
23+
ans = mid;
24+
high = mid - 1;
25+
} else {
26+
low = mid + 1;
27+
}
28+
}
29+
return ans;
30+
}
31+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /