Skip to content

Koko Eating Bananas

MediumBinary Search
Decode it first. Clues, constraints, pattern and plan before any code.

There are piles of bananas, piles[i] in pile i, and h hours until the guards return. Each hour Koko picks one pile and eats up to k bananas from it. If the pile has fewer than k left, she finishes it and waits out the rest of that hour. Return the smallest whole-number speed k that lets her eat every banana within h hours.

piles = [3, 6, 7, 11],       h = 8   →  4
piles = [30, 11, 23, 4, 20], h = 5   →  30
piles = [30, 11, 23, 4, 20], h = 6   →  23

A pile of p bananas takes ceil(p / k) hours.

Constraints

  • 1 <= piles.length <= 10^4
  • piles.length <= h <= 10^9
  • 1 <= piles[i] <= 10^9