Capacity To Ship Packages Within D Days

Medium

Topics
ArrayBinary Search

Package weights nums must be shipped in order within target days. Each day loads a contiguous prefix whose total weight is at most the ship capacity. Return the minimum capacity that ships everything within target days.

Example 1

102132435465768798109
The search space is the ship’s capacity, not an index. The smallest capacity that fits 5 days is 15.
Cells are the array values; the small numbers underneath are indices.
Input:  weights = [1,2,3,4,5,6,7,8,9,10], days = 5
Output: 15

Example 2

302122431445
Input:  weights = [3,2,2,4,1,4], days = 3
Output: 6

Constraints

  • 1 <= nums.length <= 5*10^4
  • 1 <= target <= nums.length
  • 1 <= nums[i] <= 500
Editorial — intuition, approach, complexity and pitfalls

Intuition

Package order is fixed, so a capacity determines the shipping plan entirely: load greedily until the next package would overflow, then start a new day. That makes the number of days a function of capacity — and a monotonically non-increasing one, since a bigger ship never needs more days.

Monotonic means binary searchable. Rather than searching for the plan, search for the capacity: the smallest value whose greedy plan fits within the day limit.

The bounds are what make the search correct. The lower bound is max(weights) — any smaller ship cannot carry the heaviest package at all. The upper bound is sum(weights), one day for everything.

💡 Same pattern as "divide chocolate" and "split array largest sum": binary search on the answer, with a linear greedy feasibility check per probe. Recognise it whenever the task is to minimise a maximum or maximise a minimum.

Approach

  1. lo = max(weights), hi = sum(weights).
  2. For each mid, greedily count days; if it fits the limit, search lower, else higher.
  3. The loop shape while lo < hi with hi = mid converges on the smallest feasible capacity.

Dry Run

weights = [1,2,3,4,5,6,7,8,9,10], days = 5
lo = 10, hi = 55

mid=32  plan: [1..8]=36>32 so [1..7]=28, then [8,9,10]=27
        -> 2 days <= 5   feasible, hi=32
mid=21  [1..6]=21, [7,8]=15, [9,10]=19   -> 3 days  feasible, hi=21
mid=15  [1,2,3,4,5]=15, [6,7]=13, [8]=8... -> 5 days feasible, hi=15
mid=12  [1,2,3,4]=10, [5,6]=11, [7]=7, [8]=8, [9]=9, [10]=10 -> 6 days
        not feasible, lo=13
mid=14  -> 5 days feasible, hi=14
mid=13  -> 6 days not feasible, lo=14
lo == hi == 15? converges to 15

answer 15

Complexity

  • Time — O(n log(sum)): each feasibility check is one linear pass, and the search halves a range bounded by the total weight. At the limits that is about 25 probes over 5*10^4 packages.
  • Space — O(1): the greedy check allocates nothing.

Common Pitfalls

  • Starting lo at 1 rather than max(weights). The search still converges, but every probe below the heaviest package is infeasible for a reason the greedy check must then discover.
  • Writing the greedy to split when the running load reaches capacity rather than exceeds it, which wastes a day.
  • Mixing loop shapes: while lo < hi pairs with hi = mid. Using hi = mid - 1 here can skip the answer.
  • Searching over the number of days instead of the capacity. Days are the constraint, not the unknown.

Full Solution — all languages

class Solution:
    def shipWithinDays(self, nums, target):
        def ok(cap):
            d, cur = 1, 0
            for w in nums:
                if cur + w > cap:
                    d += 1
                    cur = 0
                cur += w
            return d <= target
        lo, hi = max(nums), sum(nums)
        while lo < hi:
            mid = (lo + hi) // 2
            if ok(mid):
                hi = mid
            else:
                lo = mid + 1
        return lo
SensAI
Beta
Tap the mic to stop voice mode