-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeWork05_ship_package_with_d_days.cpp
More file actions
63 lines (47 loc) · 1.51 KB
/
HomeWork05_ship_package_with_d_days.cpp
File metadata and controls
63 lines (47 loc) · 1.51 KB
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
class Solution {
public:
int shipWithinDays(vector<int>& weights, int days) {
//D 增大 最低运载能力在 减小
//D 的最大值 为 weights.size() 最低运载能力即为 max(weights)
//D 最小为 1 最低运载能力为 weights[i] 累加
//target 值为五
int left = 0;
int right = 0;
for(int i: weights){
left = max(left,i);
right += i;
}
while(left < right){
int mid = (left + right)>>1;
int need_day = days_with_load(weights,mid);
//cout << "left: "<< left << " right: "<< right << " mid: "<<mid << " needday: " << need_day << endl;
if(need_day <= days ){
right = mid;
}
else{
left = mid + 1;
}
}
//if(left == right) cout << "hello";
return right;
}
int days_with_load(vector<int>& weights, int load){
int weight = 0;
int days = 0;
for(int i = 0; i < weights.size(); i ++){
//cout << weights[i];
if(weight + weights[i] >= load){
//cout << weight << " ";
days++;
if(weight + weights[i] == load) weight = 0;
else{ weight = weights[i]; }
//cout << endl;
}
else{
weight+=weights[i];
}
}
if(weight != 0) ++days;
return days;
}
};