-
Notifications
You must be signed in to change notification settings - Fork 0
/
1011. Capacity To Ship Packages Within D Days
65 lines (50 loc) · 1.68 KB
/
1011. Capacity To Ship Packages Within D Days
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
//intuitive top down memo TLE
class Solution {
vector<vector<unordered_map<int, int>>> dp;
int helper(vector<int> &weights, int d, int ind, int cap) {
if(d == 0 and ind >= weights.size())
return cap;
if(d < 0 or ind >= weights.size())
return INT_MAX;
if(dp[ind][d].find(cap) != dp[ind][d].end())
return dp[ind][d][cap];
int s = 0, ans = INT_MAX;
for(int i = ind; i < weights.size(); i++) {
s += weights[i];
ans = min(ans, helper(weights, d - 1, i + 1, max(cap, s)));
}
return dp[ind][d][cap] = ans;
}
public:
int shipWithinDays(vector<int>& weights, int D) {
dp.resize(weights.size(), vector<unordered_map<int, int>>(D + 1));
return helper(weights, D, 0, 0);
}
};
//binary search nlogn
class Solution {
bool canShip(vector<int> &weights, int cap, int const& d) {
int daysneed = 1, curw = 0;
for(int &i : weights) {
curw += i;
if(curw > cap) {
daysneed += 1;
curw = i;
}
}
return daysneed <= d;
}
public:
int shipWithinDays(vector<int>& weights, int D) {
int left = *max_element(weights.begin(), weights.end());
int right = accumulate(weights.begin(), weights.end(), 0);
while(left < right) {
int mid = (left + right) >> 1;
if(canShip(weights, mid, D))
right = mid;
else
left = mid + 1;
}
return left;
}
};