-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1011.cpp
More file actions
41 lines (39 loc) · 1.04 KB
/
1011.cpp
File metadata and controls
41 lines (39 loc) · 1.04 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
35
36
37
38
39
40
41
class Solution {
public:
bool check(vector<int>& weights, int D, int x){
int n = weights.size();
int curr = 0;
int count = 0;
for(int i=0; i<n; i++){
curr += weights[i];
if(curr<=x)
continue;
curr = weights[i];
++count;
if(count>D || curr>x)
return false;
}
if(count+1>D || curr>x)
return false;
return true;
}
int shipWithinDays(vector<int>& weights, int D) {
int n = weights.size();
int low = weights[0];
int high = weights[0];
for(int i=1; i<n; i++){
if(weights[i]>low)
low = weights[i];
high += weights[i];
}
while(low<high){
int mid = low + (high-low)/2;
// cout<<low<<" "<<high<<" (low,high)\t" << mid << "\n";
if(check(weights,D,mid))
high = mid;
else
low = mid+1;
}
return high;
}
};