Capacity To Ship Packages Within D Days
Packages with given weights have to be shipped in their order within a number of days. Each day the ship takes the next packages in line, as long as their total weight fits its capacity. Find the smallest capacity that ships everything in time. Our example: weights [3, 2, 2, 4, 1, 4], 3 days — the answer is 6: [3, 2], [2, 4], [1, 4].
- input
weights: the packages’ weights, in the order they are shipped;days: the days there are. Each day the ship takes the next packages in line while their total weight fits its capacity.- returns
- The smallest capacity that ships every package within
daysdays. - constraints
- 1 ≤ days ≤ weights.length ≤ 10⁴
- 1 ≤ weights[i] ≤ 500
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {number[]} weights
* @param {number} days
* @return {number}
*/
function shipWithinDays(weights, days) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more binary search problems
- 704Binary SearchEasysolve
- 35Search Insert PositionEasysolve
- 69Sqrt(x)Easysolve
- 153Find Minimum in Rotated Sorted ArrayMediumsolve
- 33Search in Rotated Sorted ArrayMediumsolve
- 875Koko Eating BananasMediumsolve
- 274H-IndexMediumsolve
- 74Search a 2D MatrixMediumsolve
- 162Find Peak ElementMediumsolve
- 34Find First and Last Position of Element in Sorted ArrayMediumsolve
- 4Median of Two Sorted ArraysHardsolve
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.