Practice

Koko Eating Bananas

Module 13 · Binary Search

Problem

Koko has piles[i] bananas in pile i. She eats at a constant speed of k bananas per hour: from any pile, she eats min(k, pile size) that hour (never combining piles in one hour). Given h hours until the guards return, find the minimum integer speed k that lets her eat all bananas within h hours.

Examples

Example 1

Inputpiles = [3,6,7,11], h = 8Output4

Example 2

Inputpiles = [30,11,23,4,20], h = 5Output30

Example 3

Inputpiles = [30,11,23,4,20], h = 6Output23

Constraints

1 ≤ piles.length ≤ 10⁴ · 1 ≤ piles[i] ≤ 10⁹ · piles.length ≤ h ≤ 10⁹.

Attempt it first

This is the module's textbook example of binary search on the answer — recognize the three-signal pattern from the concept lesson before writing anything: minimum value satisfying a condition, a feasibility check, monotonic feasibility.