So by reading the heading, what comes to your mind?
Someone named Koko is eating bananas, right?
Exactly — and the problem statement is pretty much about that.
The problem says there is a monkey named Koko who loves to eat bananas. We are given an array of banana piles, where each element represents the number of bananas in a pile.
Our task is to find the minimum number of bananas Koko should eat per hour so that she can finish all the bananas within k hours.
First Thought on Reading the Problem
Once you read the problem, what does it remind you of?
Something we’ve already done before, right?
Minimum Time to Complete Trips
So the first advice is:
Try to attempt the problem yourself before jumping to the solution.
Key Observation
If you look closely, this is a monotonic problem:
- If Koko can eat all bananas at speed
x - then she can also eat all bananas at any speed greater than
x.
This monotonic behavior immediately hints toward Binary Search on the Answer.
The problem is very similar to Minimum Time to Complete Trips, with just a few extra checks and a small formula adjustment.
Solution Approach
- Binary Search Boundaries
- Lower bound (
lb) =1→ Because Koko can eat at least one banana per hour - Upper bound (
ub) =maxPile→ At most, she eats the largest pile in one hour
- Binary Search Process
- Compute
midas a candidate eating speed - Check if it is possible to eat all bananas at this speed within
khours
- Decision Making
* If it **is possible**:
- Reduce the upper bound: `ub = mid`
- Because *“Itne mein toh ho hi jaega”*
-> Now search for a better (smaller) speed
-
If it is not possible:
- Increase the lower bound:
lb = mid + 1 - So that Koko eats a bit more bananas per hour
- Increase the lower bound:
Feasibility Check Logic
For each pile:
- Calculate how many full hours are needed using division
- If some bananas remain, add one extra hour
- Keep accumulating total time
- If at any point
time > k, returnfalseimmediately
Code (Java)
class Solution {
public static boolean isPossible(int[] piles, int bananaAte, int k) {
int time = 0;
for (int pile : piles) {
time += pile / bananaAte;
if (pile % bananaAte != 0) {
time++;
}
if (time > k) return false;
}
return true;
}
public int kokoEat(int[] arr, int k) {
int max = Integer.MIN_VALUE;
for (int val : arr) {
max = Math.max(val, max);
}
int l = 1;
int r = max;
while (l < r) {
int mid = l + (r - l) / 2;
if (isPossible(arr, mid, k)) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
}
Time Complexity
Time Complexity: O(n log maxPile)
Explanation:
Let:
-
n= number of piles -
maxPile= maximum bananas in a pile
Binary Search
- Search space:
1 → maxPile - Time taken:
O(log maxPile)
Feasibility Check (isPossible)
- Traverses all piles
- Time taken:
O(n)
Total
O(n log maxPile)
Space Complexity
Space Complexity: O(1)
- No extra data structures used
- Only a few variables
Final Words
So that’s all about the Koko Eating Bananas problem
If you’re still reading my blog till here —
Thank you so much, buddy!
Please make sure to share it with your code fellows.
Happy Coding!!
If you want, I can also:
- make it more beginner-friendly
- shorten it for LinkedIn/LeetCode discussion
- or add visual examples for better intuition
Top comments (0)