Skip to content

Commit 819676c

Browse files
author
Hieu Luong
committed
Solve problem 1011 - Capacity To Ship Packages Within D Days
1 parent cfe5e1a commit 819676c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package binarysearch;
2+
3+
public class Problem1011_CapacityToShipPackagesWithinDDays {
4+
5+
public int shipWithinDays(int[] weights, int D) {
6+
int lo = 0, hi = 0;
7+
for (int w: weights) {
8+
lo = Math.max(lo, w);
9+
hi += w;
10+
}
11+
while (lo <= hi) {
12+
int mid = lo + (hi-lo) / 2, need = 1, cur = 0;
13+
for (int w: weights) {
14+
if (cur + w > mid) {
15+
need += 1;
16+
cur = 0;
17+
}
18+
cur += w;
19+
}
20+
if (need <= D) hi = mid - 1;
21+
else lo = mid+1;
22+
}
23+
return lo;
24+
}
25+
}

0 commit comments

Comments
 (0)