-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1011.cpp
More file actions
29 lines (28 loc) · 806 Bytes
/
Copy path1011.cpp
File metadata and controls
29 lines (28 loc) · 806 Bytes
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
class Solution {
public:
int shipWithinDays(vector<int> &weights, int D) {
int low = 1, high = 25000000, mid = 0;
while (low < high) {
mid = low + (high - low) / 2;
if (Shippable(weights, mid, D))
high = mid;
else
low = mid + 1;
}
return high;
}
int Shippable(vector<int> &weights, int &capacity, int &D) {
int i = 0, day = 1;
while (i < weights.size() && day <= D) {
int residual = capacity;
while (i < weights.size() && residual - weights[i] >= 0) {
residual -= weights[i];
++i;
}
if (i >= weights.size())
return true;
++day;
}
return false;
}
};