Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dffee50

Browse files
committed
Time: 9 ms (95.22%), Space: 54.5 MB (5.90%) - LeetHub
1 parent 4d49f4f commit dffee50

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
3+
public int shipWithinDays(int[] weights, int days) {
4+
int totalLoad = 0, maxLoad = 0;
5+
for (int weight : weights) {
6+
totalLoad += weight;
7+
maxLoad = Math.max(maxLoad, weight);
8+
}
9+
10+
int low = maxLoad;
11+
int high = totalLoad;
12+
while (low < high) {
13+
int mid = (low + high) / 2;
14+
if (possible(weights, mid, days)) {
15+
high = mid;
16+
} else {
17+
low = mid + 1;
18+
}
19+
}
20+
21+
return low;
22+
}
23+
24+
private boolean possible (int[] weights, int capacity, int days) {
25+
int daysNeeded = 1, currentLoad = 0;
26+
for (int weight : weights) {
27+
currentLoad += weight;
28+
if (currentLoad > capacity) {
29+
daysNeeded++;
30+
currentLoad = weight;
31+
}
32+
}
33+
return daysNeeded <= days;
34+
}
35+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /