LeetCode Β· #1011

Capacity To Ship Packages Within D Days Solution

Find the minimum ship capacity that lets us deliver all packages in order within a fixed number of days.

01
Section One Β· Problem

Problem Statement

🏷️

Difficulty

Medium

πŸ”—

LeetCode

Problem #1011

πŸ—οΈ

Pattern

Binary Search β€” search on answer

Packages must stay in their original order. If the ship capacity is C, we greedily load as many consecutive packages as fit each day. We want the smallest capacity whose simulation finishes within days.

Example
Input: weights = [1,2,3,4,5,6,7,8,9,10], days = 5 Output: 15 // Capacity 15 works: // Day1: 1+2+3+4+5 = 15 // Day2: 6+7 = 13 // Day3: 8 // Day4: 9 // Day5: 10
02
Section Two Β· Approach 1

Test Every Capacity β€” O(n Β· sum(weights))

We could try capacities starting from max(weights) upward, simulate shipping for each, and return the first one that works.

Problem: The answer range can be huge. But the feasibility is monotonic: if capacity C works, every larger capacity also works. That monotonic boundary is exactly what binary search needs.
03
Section Three Β· Approach 2

Binary Search on Capacity β€” O(n log(sum-max))

The minimum possible capacity is max(weights) because the heaviest package must fit. The maximum possible capacity is sum(weights) because one day could carry everything. Binary search that interval and use a greedy simulation as the feasibility function.

πŸ’‘ Mental model: Think of capacity as a volume knob. Too low means you need too many days. High enough means the schedule fits. Binary search locates the first knob setting where the schedule becomes feasible.
  • Search space: [max(weights), sum(weights)]
  • Feasibility check: greedily pack consecutive weights until the next one would overflow capacity, then start a new day
  • If the days used are <= days, the capacity is feasible
  • Feasible β†’ search left for a smaller capacity
  • Infeasible β†’ search right for a larger capacity
Recognition signal:
  • β€œMinimum capacity / speed / rate / threshold such that a greedy simulation works” is the classic binary-search-on-answer pattern.
  • Write the monotonic check first, then search the answer space.
04
Section Four Β· Trace

Visual Walkthrough

Trace the sample input.

LC 1011 β€” binary search on ship capacity
Range starts at [10, 55] because max(weights)=10 and sum(weights)=55 Test capacity 32 β†’ finishes in fewer than 5 days, so search left Eventually test 15 β†’ feasible, test 14 β†’ not feasible minimum feasible capacity = 15
05
Section Five Β· Implementation

Code β€” Java & Python

Java β€” binary search + greedy day counter
class Solution { public int shipWithinDays(int[] weights, int days) { int left = 0, right = 0; for (int weight : weights) { left = Math.max(left, weight); right += weight; } while (left < right) { int mid = left + (right - left) / 2; if (canShip(weights, days, mid)) right = mid; else left = mid + 1; } return left; } private boolean canShip(int[] weights, int days, int capacity) { int usedDays = 1; int current = 0; for (int weight : weights) { if (current + weight > capacity) { usedDays++; current = 0; } current += weight; } return usedDays <= days; } }
Python β€” search the first feasible capacity
class Solution: def shipWithinDays(self, weights: list[int], days: int) -> int: left, right = max(weights), sum(weights) def can_ship(capacity: int) -> bool: used_days = 1 current = 0 for weight in weights: if current + weight > capacity: used_days += 1 current = 0 current += weight return used_days <= days while left < right: mid = (left + right) // 2 if can_ship(mid): right = mid else: left = mid + 1 return left
06
Section Six Β· Analysis

Complexity Analysis

ApproachTimeSpaceTrade-off
Test every capacityO(n Β· sum(weights))O(1)Too slow when the answer range is large.
Binary search on answer ← optimalO(n log(sum(weights) - max(weights)))O(1)Each candidate capacity is checked with one greedy scan.
07
Section Seven Β· Edge Cases

Edge Cases & Pitfalls

CaseExpected behaviorWhy it matters
One package heavier than capacityImpossible for that capacityThis is why the lower bound is max(weights).
days == 1Answer is sum(weights)Everything must ship together.
days == weights.lengthAnswer is max(weights)You can ship at most one package per day.
Reordering packagesNot allowedThe greedy simulation must preserve the original order.
Feasible midSearch leftWe need the minimum feasible capacity, not any feasible capacity.
⚠ Common Mistake: Resetting the day count incorrectly or trying to sort the weights. The order is fixed, so the only valid simulation is one left-to-right pass that starts a new day exactly when the next package would overflow the ship.

← Back to Binary Search problems