-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdsa leetcode question 1011.java
More file actions
41 lines (36 loc) · 1020 Bytes
/
dsa leetcode question 1011.java
File metadata and controls
41 lines (36 loc) · 1020 Bytes
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
dsa.java
//leetcode 1011
public boolean isPossibleToShip(int[] weight,int capacity,int days,int totalWeightPerDay) {
int d = 0;
for(int w: weight) {
totalWeightPerDay+=w;
if (totalWeightPerDay>capacity) {
d++;
totalWeightPerDay=w;
}
if(d>days)
return false;
}
return true;
}
public int shipWithinDays(int[] weights, int days,int ei) {
int maxEle =0;
for(int w:weights) {
maxEle = Math.max(maxEle,w);
int sum;
sum+= w;
}
int si = maxEle;
int sum;
ei =sum;
while(si<=ei) {
int capacity = (si+ei)/2;
if (!isPossibleToShip(weights,capacity))
si = capacity+1;
else {
ei = capacity;
}
return si;
}
}
}