Problem
FAANG · LeetCode #875

Koko Eating Bananas — three-stage drill

1The ProblemMedium

Koko Eating Bananas

Smallest eating speed to finish all banana piles within h hours.

speed 4 = eat up to 4 bananas an hour, from one pile onlyleftover capacity (dashed) is wasted — you can't start the next pile in the same hour31 hr62 hrs72 hrs113 hrshrs:1 + 2 + 2 + 3 = 8 hrs ≤ 8 ✓hrs needed27151088655554speed1✗2✗3✗4✓5✓6✓7✓8✓9✓10✓11✓↑ slowest ✓ = 4return4
Examples
piles=[3,6,7,11], h=8→ 4piles=[30,11,23,4,20], h=5→ 30

constraintAnswer (speed) lies in [1, max pile]; feasibility is monotonic.

2The PatternTIME O(log n) · SPACE O(1)

Binary Search

Halve the search space each step — check the middle, keep the half that can hold the answer.

check the middle, discard the half that can't hold the target1031527394115136157left›right↑ mid
Problems using this pattern
3The SolutionRESULT → 4

Step through it

piles = [3,6,7,11] · h = 8speed1234567891011leftright
search the eating speed in [1, 11]. h = 8 hours
Step 1 / 6
Arrow keys to navigate · escape to reset

All problems by pattern

Backtracking

Recursive Descent