Community Problem

Capacity to Ship Packages Within D Days

Difficulty: Medium

Binary-search-on-answer for the smallest belt capacity that ships all packages in order within D days, with a linear feasibility check per candidate.

Capacity to Ship Packages Within D Days

Binary-search-on-answer for the smallest belt capacity that ships all packages in order within D days, with a linear feasibility check per candidate.

MEDIUM
Free
binary-search
arrays
greedy
theowatanabe

By @theowatanabe

January 26, 2026

·

Updated May 18, 2026

282 views

2

4.5 (12)

Asked to me twice in 2024 Amazon onsites and once in a Stripe loop, and the framing every time is the same: the candidate first reaches for DP, then realizes the predicate is monotone, then writes the binary-search-on-answer template. The trick is picking the right [lo, hi] interval so the predicate boundary is reachable.

Capacity to Ship Packages Within D Days

A conveyor belt has packages that must be shipped from one port to another within days days. The i-th package on the belt has weight weights[i]. Each day, the ship loads packages from the belt in the given order, and the total weight loaded that day cannot exceed the ship's capacity. Return the least capacity of the ship that allows all packages on the belt to be shipped within days days.

Examples

Example 1:

  • Input: weights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], days = 5
  • Output: 15
  • Explanation: A capacity of 15 lets you ship [1, 2, 3, 4, 5] on day 1, [6, 7] on day 2, [8] on day 3, [9] on day 4, [10] on day 5. Capacity 14 is not enough.

Example 2:

  • Input: weights = [3, 2, 2, 4, 1, 4], days = 3
  • Output: 6
  • Explanation: Capacity 6 ships [3, 2], [2, 4], [1, 4]. Capacity 5 cannot.

Example 3:

  • Input: weights = [1, 2, 3, 1, 1], days = 4
  • Output: 3
  • Explanation: One feasible split: [1, 2], [3], [1, 1], then a fourth day is unused; capacity 2 cannot fit the 3 package.

Example 4:

  • Input: weights = [1, 2, 3], days = 3
  • Output: 3
  • Explanation: Each package on its own day; the heaviest single package sets the floor.

Constraints

  • 1 <= days <= weights.length <= 5 * 10^4
  • 1 <= weights[i] <= 500

Follow-up

Why is lo = max(weights) and hi = sum(weights) the natural search interval? What goes wrong if you pick lo = 1?

Solution

Hints

0/4
Hint 1
Hint 2
Hint 3
Hint 4
All Problems