Problem · Array

Koko Eating Bananas

Learn this problem
MediumTekion logoTekionFULLTIMEONSITE INTERVIEW

Problem statement

You are given an array piles of positive banana counts and an integer h. Koko chooses one non-empty pile each hour and eats up to k bananas from it. If a pile contains fewer than k bananas, she empties that pile and does not begin another pile during the same hour.

Return the minimum positive integer speed k that lets Koko empty all piles within at most h hours.

Function

minEatingSpeed(piles: int[], h: int) → int

Examples

Example 1

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

At speed 4, the piles take 1 + 2 + 2 + 3 = 8 hours. Speed 3 would require 10 hours.

Example 2

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

There are five piles and five hours, so each pile must be finished in one hour. The largest pile contains 30 bananas.

Constraints

  • 1 <= piles.length <= 10^5
  • 1 <= piles[i] <= 10^9
  • piles.length <= h <= 10^9

More Tekion problems

drafts saved locally
public int minEatingSpeed(int[] piles, int h) {
    // write your code here
}
piles[3,6,7,11]
h8
expected4
checking account