Skip to content
Decoder

Koko Eating Bananas

Medium

Problem

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.

Example 1

Input
piles = [3, 6, 7, 11], h = 8
Output
4

Example 2

Input
piles = [30, 11, 23, 4, 20], h = 5
Output
30

Example 3

Input
piles = [30, 11, 23, 4, 20], h = 6
Output
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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.