Capacity To Ship Packages Within D Days
Find the smallest ship capacity that delivers every package, in order, within the given number of days.
The problem
Packages with weights weights[i] must be shipped in the given order. Each day the ship takes the next packages in line as long as their total weight stays within its capacity, and packages can't be split or reordered. Return the smallest capacity that ships everything within days days.
Input weights = [3, 8, 2, 6, 4, 5], days = 3
Output 11
With capacity 11 the days carry [3, 8], [2, 6] and [4, 5]; with 10 the loads become [3], [8, 2], [6, 4], [5], which is four days.
Input weights = [2, 9, 3], days = 3
Output 9
One package a day is allowed, so the ship only needs to carry the heaviest one.
Key constraint: Up to 50,000 packages weighing up to 500 each, so the capacity can be anything up to 25 million, too many to try one by one.
How to spot the pattern
- You need the smallest capacity that makes a plan possible.
- A capacity that works keeps working when it grows, so the answers go no, no, ..., yes, yes.
- Checking one capacity is easy: load greedily day by day and count the days.
The idea
For a fixed capacity the best plan is greedy: keep loading packages onto today's ship until the next one doesn't fit, then start a new day. A bigger capacity never needs more days, so you can binary search the capacity between the heaviest package, since anything smaller can't carry it, and the total weight, which ships everything in one day. The first capacity whose greedy plan fits in days is the answer.
Brute force: Try every capacity from the heaviest package upward until the greedy plan fits, which can take O(n * s) time when the total weight is s.
Step by step
- 1Set
lo = max(weights)andhi = sum(weights). - 2While
lo < hi, takemidand count the days a ship of capacitymidneeds, loading greedily. - 3If the count is at most
days, sethi = mid; otherwise setlo = mid + 1. - 4Return
lo.
See the pattern move
The binary search pattern on a small example. Step through it, or press play.
Code
class Solution:
def shipWithinDays(self, weights: List[int], days: int) -> int:
def days_needed(capacity: int) -> int:
used, load = 1, 0
for w in weights:
if load + w > capacity: # doesn't fit: this package starts a new day
used += 1
load = 0
load += w
return used
lo, hi = max(weights), sum(weights) # from the heaviest package to everything in one day
while lo < hi:
mid = lo + (hi - lo) // 2
if days_needed(mid) <= days:
hi = mid # big enough: try smaller
else:
lo = mid + 1 # too small
return loComplexity
Time
O(n log s)
Space
O(1)
About log2(s) guesses, where s is the total weight, and each guess is one greedy pass over the n packages.
Watch out for
- Starting
lobelow the heaviest package: such a ship can never carry that package, but the greedy count would still put it on a day of its own and report a plan. - Resetting the load to 0 when a new day starts instead of to the package that didn't fit, which drops that package from the count.
Follow-up questions
How is this the same as Koko Eating Bananas?
Both ask for the smallest value that makes a check pass, and the check only gets easier as the value grows. Write the check, find a range that must contain the answer, and binary search for the first value that passes.
What if you must split an array into exactly k parts and make the largest part as small as possible?
That is Split Array Largest Sum, the same problem in different words: binary search on the largest part, and count parts greedily for each guess.
In Hinglish
Capacity par binary search karo; har capacity ke liye greedy tareeke se din gino, aur sabse chhoti capacity dhoondo jo days mein fit ho jaaye.