Skip to content
BASA DSA

Koko Eating Bananas

MediumBinary Search

An eater chooses an integer speed k. During each hour they eat up to k bananas from one pile, finishing that pile or leaving its remainder; unused time cannot be spent on another pile. Return the smallest speed that finishes within h hours.

Example 1

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

Explanation

Speed 4 needs 1+2+3=6 hours; speed 3 needs 8, so 4 is minimal.

Example 2

Input
piles = [9,3]
h = 2
Output
9

Explanation

Two hours for two piles means each must finish in one hour, requiring speed 9.

Example 3

Input
piles = [5]
h = 7
Output
1

Explanation

Speed 1 finishes five bananas within seven hours and is the smallest positive speed.

Constraints

  • 1 <= len(piles) <= 10000
  • len(piles) <= h <= 1000000000
  • Piles are 1..1000000000

Function contractminEatingSpeedArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…