Try the Problem
You can practice the problem here:
https://leetcode.com/problems/koko-eating-bananas/
Problem Description
Koko loves eating bananas 🍌.
You are given n piles of bananas, where the i-th pile contains piles[i] bananas.
The guards have left and will return after h hours. Koko wants to finish eating all the bananas before the guards come back.
Koko can choose her banana-eating speed k bananas per hour.
However, there are some important rules:
- Every hour, Koko chooses one pile of bananas.
- She eats k bananas from that pile.
- If the pile contains fewer than k bananas, she eats all of them and stops for that hour.
- She cannot move to another pile in the same hour.
Your task is to determine the minimum integer value of k such that Koko can finish all the bananas within h hours.
Example Walkthrough
Example 1
Input
Output
Explanation:
If Koko eats 4 bananas per hour, the time taken for each pile is:
Total time required:
Since Koko finishes all bananas within 8 hours, the minimum speed is 4 bananas per hour.
Example 2
Input
Output
Example 3
Input
Output
Constraints
Important observations:
- A pile may contain up to one billion bananas.
- The number of hours can also be extremely large.
- A naive solution may become computationally expensive.
Intuition Behind the Problem
The key observation in this problem is the relationship between eating speed and required hours.
If Koko eats slowly, she needs more hours.
If she eats faster, she needs fewer hours.
This creates a monotonic relationship:
Because of this property, we can apply Binary Search on the answer.
Instead of testing every possible eating speed, we can efficiently search the correct speed using binary search.
Approach 1: Brute Force
Idea
Try every possible eating speed:
For each speed:
- Calculate the total hours required.
- If the hours are less than or equal to
h, return that speed.
Drawback
The maximum pile size can be:
Trying all speeds up to 10^9 would take too long.
Time Complexity
This approach is not feasible for large inputs.
Approach 2: Binary Search on Answer (Optimal Solution)
Since the answer lies between 1 and the maximum pile size, we can apply binary search.
Search Space
Minimum possible speed:
Maximum possible speed:
Binary Search Strategy
- Choose a middle value
midas the candidate eating speed. - Calculate how many hours Koko needs at this speed.
- If the hours are less than or equal to
h, try a smaller speed. - If the hours are greater than
h, increase the speed.
This continues until we find the minimum valid speed.
Java Implementation
Time Complexity
Binary Search runs in:
Each iteration calculates hours in:
Overall complexity:
Space Complexity
No extra memory is required.
Key Takeaway
This problem is a classic example of Binary Search on Answer.
Whenever a problem asks:
“Find the minimum or maximum value such that a condition is satisfied”
You should consider applying Binary Search on the answer space.
Conclusion
Instead of testing every possible eating speed, we used Binary Search to efficiently find the minimum speed that allows Koko to finish the bananas within the given number of hours.
This approach dramatically improves performance and is a common technique used in many coding interview problems involving optimization and search space reduction.




