← Visualizers
Binary Search — Answer Space LeetCode 1011

Capacity To Ship Packages Within D Days / visualized

Same shape as Koko Eating Bananas — binary search the answer, not the array. Here the answer is a ship capacity. The feasibility check is a greedy simulation: load packages onto today's shipment in order; the moment one more would overflow the capacity, start a new day instead. Fewer days needed at a bigger capacity is monotonic, so the search is valid.

Execution

idle
L
M
H
packages, in shipping order (grouped by day at current candidate capacity)
Press Run to begin.
0 / 0
Speed

Java · running line

lo mid (candidate capacity) hi new day starts feasible
O(n · log(sum − max)) time · O(1) space  ·  lo starts at max(weights) (must fit the biggest package), hi at sum(weights) (ship everything in 1 day).