Koko Eating Bananas

Medium Binary Search AI round

piles lists piles of bananas, pile i holding piles[i] of them, and h is a number of hours, at least as many as there are piles.

At a speed of k bananas an hour the piles are eaten one at a time, and no hour is shared between two piles: a pile of p bananas takes p / k hours, rounded up to a whole hour.

Return the smallest whole speed k at which every pile is eaten within h hours.

Example 1

Input: piles = [9,4,13,6], h = 7
Output: 6

At 6 bananas an hour the piles take 2, 1, 3 and 1 hours, 7 in all. At 5 an hour they would take 2, 1, 3 and 2, which is 8, one hour too many.

Example 2

Input: piles = [25,8,17], h = 3
Output: 25

Example 3

Input: piles = [5,5,5,5], h = 20
Output: 1

Constraints

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

Watch the explanation

More Binary Search

All Binary Search problems