Find the minimum eating speed to finish all bananas in h hours.
This problem follows the Binary Search on Answer pattern, commonly found in the Binary Search category. Recognizing this pattern is key to solving it efficiently in an interview setting.
Binary search on speed. For each speed, check if total hours <= h.
Binary search on the answer space, not the data — the monotonic property is: if speed k works, any speed > k also works.
left, right = 1, max(piles)
while left < right:
mid = (left + right) // 2
hours = sum(ceil(p / mid) for p in piles)
if hours <= h:
right = mid
else:
left = mid + 1
return leftPractice Koko Eating Bananas and similar Binary Search problems with flashcards. Build pattern recognition through active recall.
Practice this problem