mediumBinary SearchBinary Search

Capacity To Ship Packages Within D Days

mediumTime: O(n log(sum))Space: O(1)

Recognize the pattern

minimize the maximum capacitymust ship within D daysbinary search on answer

Brute force idea

A straightforward first read of Capacity To Ship Packages Within D Days is this: Try every capacity from max(weights) to sum(weights). Linear search through all feasible capacities. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

Better approach

The deeper shift in Capacity To Ship Packages Within D Days is this: Binary search on the capacity. For each candidate, greedily simulate shipping: pack packages until the day's capacity is full, then start a new day. If you need ≤ D days, the capacity works. Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.

Key invariant

At the center of Capacity To Ship Packages Within D Days is one steady idea: If capacity c works (ships in ≤ D days), any capacity > c also works. This monotonicity validates binary search. The minimum valid capacity is the answer. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.

Watch out for

One easy way to drift off course in Capacity To Ship Packages Within D Days is this: Setting the lower bound to 1 instead of max(weights) — you can't split a package, so the minimum capacity must be at least the heaviest package. The fix is usually to return to the meaning of each move, not just the steps themselves.

Binary Search Pattern