-
Notifications
You must be signed in to change notification settings - Fork 359
/
s1.cpp
30 lines (30 loc) · 884 Bytes
/
s1.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
// OJ: https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/
// Author: github.com/lzl124631x
// Time: O(W * L) where W is the sum of all weights, and L is the length of `weights`.
// Space: O(1)
class Solution {
private:
bool ok(vector<int>& weights, int D, int capacity) {
int d = 1, c = 0;
for (int w : weights) {
if (c + w > capacity) {
c = 0;
++d;
if (d > D) return false;
}
c += w;
}
return true;
}
public:
int shipWithinDays(vector<int>& weights, int D) {
int sum = 0, maxVal = 0;
for (int w : weights) {
sum += w;
maxVal = max(maxVal, w);
}
int capacity = max((sum + D - 1) / D, maxVal);
while (!ok(weights, D, capacity)) capacity++;
return capacity;
}
};