-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc1231
26 lines (26 loc) · 799 Bytes
/
lc1231
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
class Solution {
public:
int maximizeSweetness(vector<int>& sweetness, int K) {
int left = 0, right = accumulate(sweetness.begin(), sweetness.end(), 0);
while(left + 1 < right) {
int mid = left + (right - left) / 2;
if(isValidK(sweetness, mid, K + 1)) {
left = mid;
} else {
right = mid;
}
}
return isValidK(sweetness, right, K+1) ? right:left;
}
bool isValidK(const vector<int>sweetness, const int curSweet, const int K) {
int piece = 0, totalSeet = 0;
for(auto s:sweetness) {
totalSeet+=s;
if(totalSeet >= curSweet) {
piece++;
totalSeet = 0;
}
}
return piece >= K;
}
};