Skip to content

Commit f059f58

Browse files
authored
Create 1014-capacity-to-ship-packages-within-d-days.js
1 parent cb6e852 commit f059f58

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+
/**
2+
* @param {number[]} weights
3+
* @param {number} D
4+
* @return {number}
5+
*/
6+
const shipWithinDays = function(weights, D) {
7+
let left = 0, right = 0;
8+
for (let w of weights) {
9+
left = Math.max(left, w);
10+
right += w;
11+
}
12+
while (left < right) {
13+
let mid = Math.floor((left + right) / 2), need = 1, cur = 0;
14+
for (let w of weights) {
15+
if (cur + w > mid) {
16+
need += 1;
17+
cur = 0;
18+
}
19+
cur += w;
20+
}
21+
if (need > D) left = mid + 1;
22+
else right = mid;
23+
}
24+
return left;
25+
}

0 commit comments

Comments
 (0)