Skip to content
AI360Xpert
Beta
Difficulty: MediumBinary Search

Koko Eating Bananas

Problem in Plain English

Given banana piles and a deadline of h hours, find the slowest hourly eating speed that still finishes every pile in time. Feasibility only improves as speed rises, so the minimum feasible speed is found by binary search.

Problem Statement

Koko loves to eat bananas. There are n piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in h hours.

Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas during this hour. Return the minimum integer k such that she can eat all the bananas within h hours.

Constraints

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

Examples

Example 1

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

At 4 bananas per hour the piles take 1, 2, 2 and 3 hours, totalling exactly 8 hours. Speed 3 needs 1 + 2 + 3 + 4 = 10 hours, so 4 is the minimum, not just a feasible choice.

Example 2

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

With only 5 hours for 5 piles each pile must finish in one hour, so the speed must cover the largest pile.

Example 3

Input
piles = [30,11,23,4,20], h = 6
Output
23

At 23 bananas per hour the piles take 2, 1, 1, 1 and 1 hours, totalling exactly 6 hours. Speed 22 needs 2 + 1 + 2 + 1 + 1 = 7 hours, so 23 is the first feasible speed.

Example 4

Input
piles = [1], h = 1
Output
1

One banana and one hour give the smallest search interval [1, 1]; speed 1 finishes exactly on the deadline.

Example 5

Input
piles = [4,4], h = 3
Output
4

Speed 3 still takes two hours per pile, totalling 4. Speed 4 takes one hour per pile. Unused capacity in one hour cannot move to the next pile.

Intuition

A pile needs whole hours: at speed 4, a pile of 7 takes two hours, even though the second hour is partly unused. That spare capacity cannot be spent on another pile. Therefore total hours are the sum of each pile's ceiling division, not the ceiling of the total bananas. Example 1 needs 1 + 2 + 2 + 3 = 8 hours at speed 4; speed 3 needs 10. Increasing speed can only reduce or preserve each pile's hours, creating an infeasible-then-feasible boundary to search.

Approaches

Try Every Speed

Solution Details

Reveal Try Every Speed: intuition, complexity, and code

Hints

Hint 1
The slowest possible speed is 1 and the fastest ever needed is the largest single pile.
Hint 2
If a speed finishes in time, every faster speed finishes too, so the answers form a false-then-true range.
Hint 3
Binary search that range, computing each trial speed hours with ceiling division per pile.

Edge Cases

  • When h equals the pile count, each pile must finish in one hour, so the answer is the largest pile.
  • Huge piles with a tiny trial speed make the hour total overflow 32-bit integers, so the accumulator must be 64-bit.
  • A single pile reduces the problem to ceiling division of that pile by h.
  • An exact multiple uses no extra hour: a pile of 8 at speed 4 takes 2 hours, while a pile of 9 takes 3.
  • When h is at least the total bananas, speed 1 is feasible and minimal. Extra allowed hours need not be used.
  • For 10000 piles of 1000000000 bananas and h = 10000, the answer is 1000000000. A trial at speed 1 would accumulate 10000000000000 hours.

Common Mistakes and Interview Tips

  • Using plain integer division instead of ceiling division, which undercounts partial hours and accepts speeds that are too slow.
  • Summing hours in a 32-bit integer, which overflows on large piles and flips a too-slow speed into a false pass.
  • Searching speeds above the largest pile, where extra speed changes nothing and the answer can never hide.
  • Taking ceil(sum(piles) / k) incorrectly shares spare hourly capacity across piles; [4, 4], h = 3 needs speed 4, not 3.
  • Returning on the first feasible middle speed finds a feasible value, not necessarily the minimum. Save it and keep searching lower.
  • Requiring hours to equal h rejects valid solutions that finish early; the feasibility test is hours <= h.
  • Interview tip: state the monotone predicate and the feasible upper bound before writing binary search. In Java promote before addition in ceiling division; long hours alone does not widen a prior int expression. Under these exact limits the numerator fits int, but explicit promotion is robust.

Key Takeaway

When a minimum feasible value has a monotone pass-fail test, binary search the answer range instead of the input: the first passing value is the optimum.