Skip to content

visual walkthrough

Koko Eating Bananas

The textbook search-on-answer problem.

Solve on LeetCode

The idea

Eating faster never takes more hours, so the speeds split into "too slow" then "fast enough".

Binary search that range for the first speed that's fast enough. Each check just adds up ⌈pile / speed⌉.

Complexity

approachtimespace
Try every speedO(max · n)O(1)
Binary search on the speedO(n log max)O(1)

More walkthroughs