Loading practice problems…
Loading your practice workspace…
Loading code editor…
16 / 29
Sign in
Algorithms/Binary Search on Answer

Koko Eating Bananas

medium35 min

Koko has several piles of bananas and h hours. At an integer speed k, she spends one hour consuming up to k bananas from one pile.

Return the smallest speed that lets her finish every pile within h hours.

Example 1

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

Output4

At speed 4, the piles take 1, 2, 2, and 3 hours, totaling 8. Speed 3 takes 10 hours, so 4 is the smallest speed that works.

Example 2

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

Output30

Five piles in five hours means each pile must fit in one hour. The largest pile has 30 bananas, so the minimum speed is 30.

Constraints

  • 1 ≤ piles.length ≤ h ≤ 100,000
  • 1 ≤ piles[i] ≤ 1,000,000,000
Binary Search on AnswerArrays