Capacity To Ship Packages Within D Days

Medium#1011search the answer

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 days days.
constraints
  • 1 ≤ days ≤ weights.length ≤ 10⁴
  • 1 ≤ weights[i] ≤ 500
our 9 tests keep to them
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

9 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more binary search problems

Pattern “Binary search” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.