◀ THE GRIND — BINARY SEARCH

Capacity to Ship Packages Within D Days

The drill: Find the smallest ship capacity that gets every package shipped within a fixed number of days — packages load in their given order, one day at a time, without ever splitting a package.

THE BRIEFING — THE FULL DRILL, IN MY OWN WORDS

A line of packages, each with its own weight, needs to move onto a ship over a fixed number of days, and the packages must load in the exact order they're given — no rearranging, no splitting a package across two days.

Each day the ship carries packages up to some fixed weight capacity; once the next package in line would push the day's load over that capacity, the day ends and a fresh day begins with that package.

The job is to find the smallest capacity that still gets every package shipped within the day limit — too small a capacity and the days run out before the line does.

EX 01
weights = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] · days = 5
30
ASCENDING WEIGHTS, TIGHT DAY BUDGET
EX 02
weights = [1, 3, 5, 2, 4, 6, 8, 7] · days = 3
15
UNSORTED WEIGHTS, ORDER MATTERS
EX 03
weights = [7] · days = 1
7
SINGLE PACKAGE, ONE DAY
THE HINTS — TAKE ONLY WHAT YOU NEED
HINT 1 THE NUDGE

A larger capacity can only ever need the same or fewer days than a smaller one — days-needed is monotonic in capacity, which is what makes bisecting the capacity itself valid.

HINT 2 THE STRUCTURE

For a candidate capacity, simulate loading packages in order, starting a new day whenever the next package would overflow the current one — count how many days that takes.

HINT 3 ONE STEP FROM THE ANSWER

Binary-search capacity between max(weights) and sum(weights): if the simulated days exceed the limit, the capacity is too small — raise it; otherwise try to shrink it.

COACH'S BOARD — THE PATTERN, STEP BY STEP
THE CARGO CEILINGPATTERN · BINARY SEARCH THE CAPACITYweights = [1, 2, 3, 4, 5] · days = 3
5
6
7
8
9
10
11
12
13
14
15
DAYS NEEDED AT THIS CAPACITY
— empty —
STEP 1

5 packages: 1, 2, 3, 4, 5. Binary-search capacity between 5 (heaviest package) and 15 (total weight) for the smallest that ships in 3 days.

STEP 1 / 10 · ← → WORK TOO
THE SPLITS — TWO PACES, TWO LANGUAGES
grind/capacity-to-ship-packages-within-d-days.pyRACE PACE
LANG ▸
PACE ▸
class Solution:
    def shipWithinDays(self, weights: List[int], days: int) -> int:
        def days_needed(capacity: int) -> int:
            d, load = 1, 0
            for w in weights:
                if load + w > capacity:
                    d += 1
                    load = 0
                load += w
            return d

        lo, hi = max(weights), sum(weights)
        while lo < hi:
            mid = (lo + hi) // 2
            if days_needed(mid) <= days:
                hi = mid
            else:
                lo = mid + 1
        return lo
TIME O(N LOG(SUM(WEIGHTS)))SPACE O(1)PYTHON · RACE PACE · 19 LN

✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED