Capacity To Ship Packages Within D Days
1 approach, code in every language
Problem description
A conveyor belt feeds a ship a sequence of packages, each with a known weight. Every day the ship is loaded with packages taken from the front of the belt in their given order, and the combined weight loaded on any single day cannot exceed the ship's capacity. The order of packages is fixed and cannot be rearranged.
Given the list of weights and a target number of days, find the smallest ship capacity that still allows every package to be shipped within that many days.
Example 1
Input: weights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], days = 5
Output: 15
With capacity 15 the loads can be (1..5), (6,7), (8), (9), (10) across five days. Any smaller capacity forces a sixth day.
Example 2
Input: weights = [4, 4, 4, 4], days = 2
Output: 8
Capacity 8 lets each day carry two boxes: (4,4) and (4,4). Capacity 7 would require three days.
Constraints
1 <= days <= weights.length <= 5 * 10^41 <= weights[i] <= 500
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Kiln Load Ceiling is our own problem on the technique behind Capacity To Ship Packages Within D Days: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free