Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-114

Koko Eating Bananas

Problem

Given piles of bananas and h hours available, return the minimum integer eating speed that finishes all piles within h hours, where only one pile can be eaten per hour. Assume h >= len(piles).

Binary search the speed, summing ceil(p / speed) hours for every pile to test feasibility.

Hint

Integer ceiling division is (p + speed - 1) // speed.

Test cases

case 1
in: [[3,6,7,11],8]
expected: 4
case 2
in: [[30,11,23,4,20],5]
expected: 30
case 3
in: [[30,11,23,4,20],6]
expected: 23
case 4
in: [[1],1]
expected: 1
case 5
in: [[1,1,1,1],4]
expected: 1
Starter codePython
def min_eating_speed(piles, h):
    # Your code here
    pass

5 test cases · run your code against them in the editor