Capacity to Ship Packages Within Days
Problem statement
Packages must be shipped in the order given by weights. Each day, load a consecutive prefix of the remaining packages without exceeding the ship's capacity.
Return the smallest positive capacity that ships every package within days days.
Function
shipWithinDays(weights: int[], days: int) → intExamples
Example 1
weights = [1,2,3,4,5,6,7,8,9,10]days = 5return = 15Capacity 15 ships the ordered packages in five days, while any smaller capacity needs more days.
Example 2
weights = [3,2,2,4,1,4]days = 3return = 6The loads can be [3,2], [2,4], and [1,4].
Example 3
weights = [1,2,3,1,1]days = 4return = 3Capacity 3 is enough and no capacity below the heaviest package can work.
Constraints
1 <= weights.length <= 50000.1 <= weights[i] <= 500.1 <= days <= weights.length.