Skip to content

Commit e4b3587

Browse files
authored
Create 1011-capacity-to-ship-packages-within-d-days.js
1 parent 2927963 commit e4b3587

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[]} weights
3+
* @param {number} days
4+
* @return {number}
5+
*/
6+
const shipWithinDays = function(weights, days) {
7+
let l = Math.max(...weights)
8+
let r = weights.reduce((ac, e) => ac + e, 0)
9+
while(l < r) {
10+
const mid = Math.floor((l + r) / 2)
11+
if(valid(mid)) {
12+
r = mid
13+
} else l = mid + 1
14+
}
15+
16+
return l
17+
18+
function valid(mid) {
19+
let res = 1, cur = 0
20+
for(let w of weights) {
21+
if(cur + w > mid) {
22+
cur = 0
23+
res++
24+
}
25+
cur += w
26+
}
27+
return res <= days
28+
}
29+
};

0 commit comments

Comments
 (0)