-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCapacityToShipPackagesWithinDDays_1011.cpp
71 lines (54 loc) · 1.67 KB
/
CapacityToShipPackagesWithinDDays_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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
~ Author : https://leetcode.com/tridib_2003/
~ Problem : 1011. Capacity To Ship Packages Within D Days
~ Link : https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/
*/
class Solution {
public:
// Step 3: Design the predicate function
bool isPossible(int wt, vector<int>& weights, int days) {
int curr_days = 1, rem_wt = wt;
for (auto w : weights) {
if (w > rem_wt) {
if (++curr_days > days)
return false;
rem_wt = wt - w;
}
else {
rem_wt -= w;
}
}
return curr_days <= days;
}
// Step 2: Perform binary search in the search space [lo, hi]
int shipWithinDaysUtil(int lo, int hi, vector<int>& weights, int days) {
int minWeight = hi;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (isPossible(mid, weights, days)) {
minWeight = mid;
hi = mid - 1;
}
else {
lo = mid + 1;
}
}
return minWeight;
}
int shipWithinDays(vector<int>& weights, int days) {
int n = weights.size();
// Step 1: Find the lo and hi of the initital search space
int lo = INT_MIN, hi = 0;
for (auto w : weights) {
lo = max(lo, w);
hi += w;
}
return shipWithinDaysUtil(lo, hi, weights, days);
}
};
/*
~ T.C. - O(n * log k)
where, n = weights.size() and
k = weights[0] + weights[1] + ... + weights[n - 1]
~ S.C. - O(1)
*/