Skip to content

Commit e14ae82

Browse files
authored
Merge pull request #2828 from Anukriti167/main-1
Create 1011-capacity-to-ship-packages-within-d-days.java
2 parents 7c1fbdf + d35ed44 commit e14ae82

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public boolean canShip(int cap, int days, int[] weights){
3+
int ships=1, currCap=cap;
4+
for(int w: weights){
5+
if(currCap - w < 0){
6+
ships += 1;
7+
currCap = cap;
8+
}
9+
currCap -= w;
10+
}
11+
return ships <= days;
12+
}
13+
public int shipWithinDays(int[] weights, int days) {
14+
int l=0, r=0;
15+
16+
for(int w: weights){
17+
l = Math.max(l, w);
18+
r += w;
19+
}
20+
21+
int res = r;
22+
23+
while(l <= r){
24+
int cap = (l+r)/2;
25+
if(canShip(cap, days, weights)){
26+
res = Math.min(res, cap);
27+
r = cap-1;
28+
}else{
29+
l = cap+1;
30+
}
31+
}
32+
return res;
33+
}
34+
}

0 commit comments

Comments
 (0)