-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1011.java
44 lines (34 loc) · 1.02 KB
/
1011.java
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
class Solution {
public int shipWithinDays(int[] weights, int D) {
int right = 0;
int menor = -1000000000;
for(int i = 0; i < weights.length; i++){
right += weights[i];
if(menor < weights[i]){
menor = weights[i];
}
}
int left = menor;
int fm = left;
while(left <= right){
int meio = (left + right) / 2;
int dias = 0;
int aux = 0;
int ultim = -1;
for(int i = 0; i < weights.length; i++){
if(weights[i] + aux >= meio){
aux = 0;
dias++;
}
aux += weights[i];
}
if(dias >= D){
left = meio+1;
fm = meio;
}else{
right = meio - 1;
}
}
return fm;
}
}