FastPrepKoko Eating Bananas
Problem · Array

Koko Eating Bananas

Learn this problem
MediumAmazon logoAmazonINTERNONSITE INTERVIEW
See Amazon hiring insights

Problem statement

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

Return the minimum integer speed k that lets Koko empty every pile within at most h hours.

Function

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

Examples

Example 1

piles = [4,9,13]h = 8return = 4

At speed 4, the piles take 1 + 3 + 4 = 8 hours. Speed 3 needs 2 + 3 + 5 = 10 hours, so 4 is minimal.

Example 2

piles = [8,8,8]h = 3return = 8

Only one pile can be chosen per hour. With exactly three hours for three piles, each pile must be emptied in one hour, requiring speed 8.

Example 3

piles = [1,7,10,15]h = 10return = 4

Speed 4 needs 1 + 2 + 3 + 4 = 10 hours. Speed 3 needs 1 + 3 + 4 + 5 = 13 hours.

Constraints

  • 1 <= piles.length <= 100000
  • 1 <= piles[i] <= 1000000000
  • piles.length <= h <= 1000000000

More Amazon problems

drafts saved locally
public int minEatingSpeed(int[] piles, int h) {
    // write your code here
}
piles[4,9,13]
h8
expected4
checking account