Skip to content

Capacity To Ship Packages Within D Days

MediumBinary Search
Decode it first. Clues, constraints, pattern and plan before any code.

Packages with weights weights sit on a conveyor belt and must be shipped in the given order. Each day you load the ship with packages from the front of the belt until adding the next one would exceed the ship's capacity. Return the least weight capacity that gets every package shipped within days days.

weights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], days = 5   →  15
weights = [3, 2, 2, 4, 1, 4],             days = 3   →  6
weights = [1, 2, 3, 1, 1],                days = 4   →  3

With capacity 15 in the first example the days are [1..5], [6, 7], [8], [9], [10].

Constraints

  • 1 <= days <= weights.length <= 5 * 10^4
  • 1 <= weights[i] <= 500