Skip to content

visual walkthrough

Capacity To Ship Packages Within D Days

Minimise a capacity subject to a feasibility check.

Solve on LeetCode

The idea

A bigger ship never needs more days, so capacities split into "not enough" then "enough".

Binary search between the heaviest package and the total weight. Checking a capacity is a single pass: fill each day until the next package won't fit.

Complexity

approachtimespace
Try every capacityO(n · sum)O(1)
Binary search on capacityO(n log sum)O(1)

More walkthroughs