-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem-1011.cpp
36 lines (32 loc) · 937 Bytes
/
Problem-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
32
33
34
35
36
// Problem - 1011
// https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/
// O(nlogn) time complexity and O(1) space complexity solution
class Solution {
public:
bool canBeShipped(vector <int>& weights, int D, int maxCapacity) {
int days = 1;
int capacity = 0;
for(int w : weights) {
capacity += w;
if(capacity > maxCapacity) {
days++;
capacity = w;
}
}
return days <= D;
}
int shipWithinDays(vector<int>& weights, int D) {
int h = accumulate(weights.begin(), weights.end(), 0);
int l = *max_element(weights.begin(), weights.end());
while(l < h) {
int mid = (h-l)/2 + l;
if(canBeShipped(weights, D, mid)) {
h = mid;
}
else {
l = mid+1;
}
}
return l;
}
};