Capacity to Ship Packages Within D Days
Capacity to Ship Packages Within D Days is LeetCode 1011 (Medium). Packages wait on a conveyor belt in a fixed order, and package i weighs weights[i]. Each day the ship takes the next packages off the belt, in order, as long as their total stays within its capacity. Return the smallest capacity that gets every package shipped within days days.
- Up to 5·10⁴ packages, each weighing 1 to 500, and
daysis never more than the number of packages. - The total weight is at most 2.5·10⁷, so 32-bit integers are enough.
- 1 <= days <= weights.length <= 5 * 10⁴
- 1 <= weights[i] <= 500
Intuition
Trying every capacity in turn is too slow, but judging one capacity is easy: load the ship, keep taking packages while they fit, and sail when the next one would not. Counting the trips tells you how many days that capacity needs.
- Days only go down: a bigger ship never needs more days, so the capacities that work are every value from some point upward.
- Where to start: the ship must lift the heaviest package, and over
daystrips it must move the total weight, so it needs at leasttotal / daysper trip, rounded up.
Binary search between that start and the total weight for the first capacity that works.
The question asks for the smallest setting that still meets a limit, and checking one setting is a simple pass. When a bigger setting can only help, binary search the setting instead of building the answer directly.
Approach
Before reading on, load [2, 2, 5, 4, 5] onto a ship of size 8 and count the days, then try size 9. Then ask why a size that needs 2 days is fine when 3 are allowed. Aim for O(n log W), where W is the total weight.
Set the range
Let lo be the larger of max(weights) and ceil(total / days), and hi = total. Below lo, the ship either cannot lift the heaviest package or cannot move all the weight in days trips. One day carrying everything always works.
Count the days a capacity needs
Start on day 1 with an empty ship and add packages in order. When the load would pass the capacity, the ship sails and that package starts the next day. Sailing only when forced puts the most onto every day, so this gives the fewest days possible.
Stop as soon as it fails
The moment the count passes days, the capacity has failed, so stop reading the belt. A ship that is far too small fails within the first few days instead of walking all 5·10⁴ packages.
Halve the range
Try mid = (lo + hi) // 2. If it fits, set hi = mid: it works, and something smaller still might. If not, set lo = mid + 1: it fails, and so does every smaller size. When lo == hi, return it.
Code in Python, C++ and Java
Common pitfalls
Starting the search at 1
lo = 1
lo = max(max(weights), (total + days - 1) // days)
With a ship smaller than a package, the count still puts that package on a fresh day and moves on, so it counts days for a ship that can never lift it. On [1, 10] with days = 2 the search returns 1, although the ship must carry 10.
Counting days from 0
used, load = 0, 0
used, load = 1, 0
The first day has begun before any package is loaded. Starting at 0 reports one day fewer than the real plan, so sizes that need days + 1 trips pass. On [2, 5, 6, 7, 2] with days = 3 this returns 8 instead of 9.
Dropping the size that fits
if fits(mid):
hi = mid - 1if fits(mid):
hi = midWith while lo < hi, the range must keep mid when it works, because it may be the answer. Moving past it can end on a size that fails: [2, 5, 6, 7, 2] with days = 3 returns 8, which needs 4 days.
Edge cases
The lower bound ceil(total / 1) is already the total, so lo == hi before the loop starts and the answer is the whole weight in one trip.
One package per day always fits, so the answer is the heaviest package. The search reaches it because ceil(total / days) is never more than the heaviest weight here.
Complexity
Capacity to Ship Packages Within D Days FAQ
Could holding a package back for tomorrow ever save a day?
No. Take any plan that works and compare day by day. After day 1, the greedy load has shipped at least as many packages as that plan, since it only sails when the next package would not fit. If greedy is at or ahead after day d, its day d + 1 starts no later and can reach at least as far. So greedy finishes on or before the plan's last day.
What if a package could be split across two days?
Then the answer would be exactly ceil(total / days): fill each day to the brim and carry the rest over. Whole packages can only push the answer up from there, which is why that value is a safe place to start the search.
Is this the same as Split Array Largest Sum?
Yes, under another name: each day is a piece of the array, and the capacity is the largest piece sum. Split Array Largest Sum allows weights of 0, so the per-day bound ceil(total / days) is the only extra trick this page uses.