-
Notifications
You must be signed in to change notification settings - Fork 3
/
1011.cpp
31 lines (31 loc) · 866 Bytes
/
1011.cpp
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
class Solution {
public:
bool criteria(vector<int>& weights, int capacity, int days) {
int current = 0;
int count = 1;
for (auto& weight : weights) {
current += weight;
if (current > capacity) {
current = weight;
count += 1;
}
}
return count <= days;
}
int shipWithinDays(vector<int>& weights, int days) {
int minValue = INT_MIN;
int sum = 0;
for (auto& weight : weights) {
minValue = max(minValue, weight);
sum += weight;
}
int left = minValue;
int right = sum;
while (left < right) {
int mid = left + (right - left) / 2;
if (criteria(weights, mid, days)) right = mid;
else left = mid + 1;
}
return left;
}
};