LeetCode #1011 Medium

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 days is never more than the number of packages.
  • The total weight is at most 2.5·10⁷, so 32-bit integers are enough.
Constraints
  • 1 <= days <= weights.length <= 5 * 10⁴
  • 1 <= weights[i] <= 500
binary-searcharraybinary-search-on-answergreedy
Open on LeetCode ↗
02

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 days trips it must move the total weight, so it needs at least total / days per trip, rounded up.

Binary search between that start and the total weight for the first capacity that works.

How to spot this pattern

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.

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

4

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def shipWithinDays(self, weights, days):
▶3 def fits(cap):
▶4 used, load = 1, 0
▶5 for w in weights:
▶6 load += w
▶7 if load > cap:
▶8 used += 1
▶9 load = w
▶10 if used > days:
▶11 return False
▶12 return True
▶13 
▶14 total = sum(weights)
▶15 lo = max(max(weights), (total + days - 1) // days)
▶16 hi = total
▶17 while lo < hi:
▶18 mid = (lo + hi) // 2
▶19 if fits(mid):
▶20 hi = mid
▶21 else:
▶22 lo = mid + 1
▶23 return lo
belt22545ship size618daysno ship size tried yetthe answer is between 6 and 18
heaviest5
total / days6rounded up
range6–18
The ship must lift the heaviest package, 5. It must also move 18 in 3 trips, at least 6 per trip. So no size below 6 can work, and 18, everything in one day, always does.
belt22545ship size61812dayssize 122254day 1day limittry size 12: day 1 sails with 9; 4 won't fit
size12
day 19of 12
days so far1
The range is 6 to 18, so try the middle, 12. Day 1 takes 2 + 2 + 5 = 9. Adding 4 would make 13, more than 12, so the ship sails now and 4 starts day 2. Taking as much as fits each day never costs an extra day.
belt22545ship size61812dayssize 12225day 145day 2day limitbelt empty after 2 days: size 12 works
size12
days2allowed 3
range6–12
Day 2 takes 4 + 5 = 9 and the belt is empty. 2 days is within 3, so size 12 works. Using fewer days than allowed is fine: the ship may simply stay in port. A smaller size might work too, so the range shrinks to 6 to 12.
belt22545ship size618129dayssize 92254day 1day limittry size 9: day 1 sails with 9; 4 won't fit
size9
day 19of 9
days so far1
The range is 6 to 12, so try the middle, 9. Day 1 takes 2 + 2 + 5 = 9. Adding 4 would make 13, more than 9, so the ship sails now and 4 starts day 2. Taking as much as fits each day never costs an extra day.
belt22545ship size618129dayssize 9225day 145day 2day limitbelt empty after 2 days: size 9 works
size9
days2allowed 3
range6–9
Day 2 takes 4 + 5 = 9 and the belt is empty. 2 days is within 3, so size 9 works. Using fewer days than allowed is fine: the ship may simply stay in port. A smaller size might work too, so the range shrinks to 6 to 9.
belt22545ship size6181297dayssize 7225day 1day limittry size 7: day 1 sails with 4; 5 won't fit
size7
day 14of 7
days so far1
The range is 6 to 9, so try the middle, 7. Day 1 takes 2 + 2 = 4. Adding 5 would make 9, more than 7, so the ship sails now and 5 starts day 2. Taking as much as fits each day never costs an extra day.
belt22545ship size6181297dayssize 722day 154day 2day limitday 2 sails with 5; 4 won't fit
size7
day 25of 7
days so far2
Day 2 takes just 5. Adding 4 would make 9, more than 7, so the ship sails now and 4 starts day 3. Taking as much as fits each day never costs an extra day.
belt22545ship size6181297dayssize 722day 15day 24day 35day 4day limit5 needs day 4, only 3 allowed: size 7 fails
size7
days4
range8–9
Day 3 holds 4 and 5 more would make 9, over 7, so it waits for day 4. That is more than 3 days, so size 7 fails without reading the rest of the belt. Every smaller size fails too, so the range moves up to 8 to 9.
belt22545ship size61812978dayssize 8225day 1day limittry size 8: day 1 sails with 4; 5 won't fit
size8
day 14of 8
days so far1
The range is 8 to 9, so try the middle, 8. Day 1 takes 2 + 2 = 4. Adding 5 would make 9, more than 8, so the ship sails now and 5 starts day 2. Taking as much as fits each day never costs an extra day.
belt22545ship size61812978dayssize 822day 154day 2day limitday 2 sails with 5; 4 won't fit
size8
day 25of 8
days so far2
Day 2 takes just 5. Adding 4 would make 9, more than 8, so the ship sails now and 4 starts day 3. Taking as much as fits each day never costs an extra day.
belt22545ship size61812978dayssize 822day 15day 24day 35day 4day limit5 needs day 4, only 3 allowed: size 8 fails
size8
days4
range9
Day 3 holds 4 and 5 more would make 9, over 8, so it waits for day 4. That is more than 3 days, so size 8 fails without reading the rest of the belt. Every smaller size fails too, so the range moves up to 9.
belt22545ship size61812978dayssize 9225day 145day 2day limitrange closed: answer 9
answer9
The range has closed on 9. Every smaller size needed more than 3 days, and 9 does not. The loads drawn are the greedy days for this size.
05

Common pitfalls

Starting the search at 1

✗ Wrong
lo = 1
✓ Right
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

✗ Wrong
used, load = 0, 0
✓ Right
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

✗ Wrong
if fits(mid):
    hi = mid - 1
✓ Right
if fits(mid):
    hi = mid

With 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.

06

Edge cases

days is 1

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.

days equals the number of packages

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.

07

Complexity

Time
O(n log W)
Space
O(1)
n is the number of packages and W is the total weight. W is at most 2.5·10⁷, so the loop runs about 25 times, each with one pass over the belt that may stop early. Only a few integers are stored.
08

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.