-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2517.cpp
More file actions
34 lines (34 loc) · 1.03 KB
/
2517.cpp
File metadata and controls
34 lines (34 loc) · 1.03 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
class Solution {
public:
bool criteria(vector<int>& differences, int minSum, int partition) {
// once >= minSum, make partition, the max partition can be made
int part = 0;
int m = differences.size();
int currSum = 0;
for (int i = 0; i < m; ++i) {
currSum += differences[i];
if (currSum >= minSum) {
part++;
currSum = 0;
}
}
return part < partition;
}
int maximumTastiness(vector<int>& price, int k) {
sort(price.begin(), price.end());
vector<int> differences;
int n = price.size();
if (n == 1) return 0;
for (int i = 0; i < n - 1; ++i) {
differences.push_back(price[i + 1] - price[i]);
}
int left = 0;
int right = INT_MAX;
while (left < right) {
int mid = left + (right - left) / 2;
if (criteria(differences, mid, k - 1)) right = mid;
else left = mid + 1;
}
return left - 1;
}
};