875. Koko Eating Bananas LeetCode Solution
In this guide, you will get 875. Koko Eating Bananas LeetCode Solution with the best time and space complexity. The solution to Koko Eating Bananas problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Koko Eating Bananas solution in C++
- Koko Eating Bananas solution in Java
- Koko Eating Bananas solution in Python
- Additional Resources

Problem Statement of 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
Constraints:
1 <= piles.length <= 104
piles.length <= h <= 109
1 <= piles[i] <= 109
Complexity Analysis
- Time Complexity: O(n\log(\max(\texttt{piles})))
- Space Complexity: O(1)
875. Koko Eating Bananas LeetCode Solution in C++
class Solution {
public:
int minEatingSpeed(vector<int>& piles, int h) {
int l = 1;
int r = ranges::max(piles);
while (l < r) {
const int m = (l + r) / 2;
if (eatHours(piles, m) <= h)
r = m;
else
l = m + 1;
}
return l;
}
private:
// Returns the hours to eat all the piles with speed m.
int eatHours(const vector<int>& piles, int m) {
return accumulate(piles.begin(), piles.end(), 0,
[&](int subtotal, int pile) {
return subtotal + (pile - 1) / m + 1; // ceil(pile / m)
});
}
};
/* code provided by PROGIEZ */
875. Koko Eating Bananas LeetCode Solution in Java
class Solution {
public int minEatingSpeed(int[] piles, int h) {
int l = 1;
int r = Arrays.stream(piles).max().getAsInt();
while (l < r) {
final int m = (l + r) / 2;
if (eatHours(piles, m) <= h)
r = m;
else
l = m + 1;
}
return l;
}
// Returns the hours to eat all the piles with speed m.
private int eatHours(int[] piles, int m) {
return Arrays.stream(piles).reduce(
, (subtotal, pile) -> subtotal + (pile - 1) / m + 1); // ceil(pile / m)
}
}
// code provided by PROGIEZ
875. Koko Eating Bananas LeetCode Solution in Python
class Solution:
def minEatingSpeed(self, piles: list[int], h: int) -> int:
def eatHours(m: int) -> bool:
"""Returns the hours to eat all the piles with speed m."""
return sum((pile - 1) // m + 1 for pile in piles)
l = 1
r = max(piles)
return bisect.bisect_left(range(l, r), True,
key=lambda m: eatHours(m) <= h) + l
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.