Capacity To Ship Packages Within D Days Problem
Description
LeetCode Problem 1011.
A conveyor belt has packages that must be shipped from one port to another within days days.
The i^th package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.
Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within days days.
Example 1:
Input: weights = [1,2,3,4,5,6,7,8,9,10], days = 5
Output: 15
Explanation: A ship capacity of 15 is the minimum to ship all the packages in 5 days like this:
1st day: 1, 2, 3, 4, 5
2nd day: 6, 7
3rd day: 8
4th day: 9
5th day: 10
Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed.
Example 2:
Input: weights = [3,2,2,4,1,4], days = 3
Output: 6
Explanation: A ship capacity of 6 is the minimum to ship all the packages in 3 days like this:
1st day: 3, 2
2nd day: 2, 4
3rd day: 1, 4
Example 3:
Input: weights = [1,2,3,1,1], days = 4
Output: 3
Explanation:
1st day: 1
2nd day: 2
3rd day: 3
4th day: 1, 1
Constraints:
1 <= days <= weights.length <= 5 * 10^41 <= weights[i] <= 500
Solution
Approach: Binary Search on the Capacity
For a fixed capacity it is easy to find the number of days we need: load the packages in order, and start a new day whenever the next package would go over the capacity. Loading as much as possible each day is never worse, because leaving a package for later only pushes more weight onto the following days.
A bigger ship never needs more days, so the capacities that work form a range: everything from the answer upwards. That lets us binary search for the smallest capacity that works:
- The answer is at least
max(weights), otherwise the heaviest package never fits. - It is at most
sum(weights), which ships everything in one day.
For each middle capacity mid, compute the days needed. If they are at most days, the answer is mid or smaller (hi = mid); otherwise it is larger (lo = mid + 1).
Starting lo at the heaviest package matters for the greedy check: with a smaller capacity, a package that is too heavy would get a day of its own and the count would be wrong.
Complexity: O(n log S) time, where S = sum(weights), and O(1) extra space.
class Solution:
def shipWithinDays(self, weights: List[int], days: int) -> int:
def days_needed(cap: int) -> int:
"""Days to ship everything with this capacity, loading greedily in order."""
used, load = 1, 0
for w in weights:
if load + w > cap:
used += 1 # this package starts a new day
load = 0
load += w
return used
# The answer lies between the heaviest package and the total weight.
lo, hi = max(weights), sum(weights)
while lo < hi:
mid = (lo + hi) // 2
if days_needed(mid) <= days:
hi = mid # mid works, so the answer is mid or smaller
else:
lo = mid + 1
return lo