Koko Eating Bananas

Koko loves to eat bananas. There are n piles of bananas, the ith 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.

Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.

Return the minimum integer k such that she can eat all the bananas within h hours.

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

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

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


  • 1 <= piles.length <= 104
  • piles.length <= h <= 109
  • 1 <= piles[i] <= 109


We want to apply the binary search template, and implement the feasible function. Here, the feasible function is whether Koko can_finish_eating all piles within h hours while eating at speed k per hour.

Since Koko eats at only one pile during each hour, ceil(float(p)/k) is the time Koko takes to finish pile p. Note that p/k does not work here because we want a whole number of hours so we needed to round up p/k. Therefore, the feasiblity is determined by whether Koko's hours_used is within h hours, where hours_used is the total hours to finish all piles.

Recall example 1 with Input: piles = [3,6,7,11], h = 8, and Output: 4, the can_finish_eating function returns the following results.


1def can_finish_eating(piles, h, k):
2    hours_used = 0
3    for p in piles:
4        hours_used += ceil(float(p)/k)
5    return hours_used <= h
7def minEatingSpeed(piles, h):
8    left, right = 1, 1000000000 # 10^9 max length of the piles
9    ans = -1
10    while left <= right:
11      mid = (left + right) // 2
12      if can_finish_eating(piles, h, mid):
13          ans = mid
14          right = mid - 1
15      else:
16          left = mid + 1
17    return ans
Not Sure What to Study? Take the 2-min Quiz to Find Your Missing Piece:

Which data structure is used to implement recursion?

Discover Your Strengths and Weaknesses: Take Our 2-Minute Quiz to Tailor Your Study Plan:

A person thinks of a number between 1 and 1000. You may ask any number questions to them, provided that the question can be answered with either "yes" or "no".

What is the minimum number of questions you needed to ask so that you are guaranteed to know the number that the person is thinking?

Not Sure What to Study? Take the 2-min Quiz:

What are the most two important steps in writing a depth first search function? (Select 2)

Fast Track Your Learning with Our Quick Skills Quiz:

What is the best way of checking if an element exists in a sorted array once in terms of time complexity? Select the best that applies.

Recommended Readings

Got a question? Ask the Teaching Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.

TA 👨‍🏫