-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cpp
56 lines (51 loc) · 1.3 KB
/
Solution.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
#include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
private:
bool canShipWithinDays(vector<int> const& weights,
int days,
int const capacity) {
// Weights are loaded in order of occurence. No need to sort
int requiredDays = 1;
int currentLoad = 0;
for (int const& weight : weights) {
currentLoad += weight;
if (currentLoad > capacity) {
// Wait for next day
currentLoad = weight;
++requiredDays;
}
}
return requiredDays <= days;
}
public:
int shipWithinDays(vector<int>& weights, int days) {
// Insight: There exist a k weight, such that if
// canShipWithinDays(k) == true, then canShipWithinDays(k + 1) == true
// Therefore, binary search can be applied
int left = 0;
int right = 0;
for (int const& num : weights) {
left = max(left, num);
right += num;
}
while (left < right) {
int mid = left + (right - left) / 2;
if (!canShipWithinDays(weights, days, mid)) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
};