#1011

Capacity To Ship Packages Within D Days

Medium Binary search on the answer

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Packages must be shipped in the given order, with one ship trip per day. Find the smallest ship capacity that ships everything within the given number of days.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the smallest ship capacity that ships everything, in order, within the days.

Why this works: bigger capacity never needs more days. Binary search between the heaviest package and the total weight, counting days greedily.

Example: [1..10], days 5

capacity 15: [1-5] [6-7] [8] [9] [10] → 5 days ✓capacity 14 needs 6 days ✗ → answer 15

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Starting the search at 1 — capacities below the heaviest package can never work and break the greedy count.

Solve on LeetCode ↗
Complexity
  • Time: O(n log(sum)).
  • Space: O(1).
Topic: Binary Search
🎬 Episodes 21–23: video coming soon