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 <= 104piles.length <= h <= 1091 <= piles[i] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you need to figure out the slowest speed at which someone can eat all their piles of food within a given time limit. The brute force approach involves testing every single possible eating speed one by one, starting from the absolute slowest possible speed and working your way up.
Here's how the algorithm would work step-by-step:
import math
def min_eating_speed(piles, allowed_hours):
# We test every possible speed starting from one because it is the slowest possible rate
current_speed = 1
while True:
total_hours_needed = 0
for banana_pile in piles:
total_hours_needed += math.ceil(banana_pile / current_speed)
# We stop as soon as we find the first speed that finishes all food within the deadline
if total_hours_needed <= allowed_hours:
return current_speed
current_speed += 1Instead of testing every possible eating speed one by one, we can treat this like a guessing game using a range of numbers. By smartly narrowing down our guesses between the slowest and fastest possible speeds, we can quickly find the exact minimum speed needed to finish in time.
Here's how the algorithm would work step-by-step:
import math
def min_eating_speed(banana_piles, max_allowed_hours):
lowest_possible_speed = 1
highest_possible_speed = max(banana_piles)
optimal_eating_speed = highest_possible_speed
# Binary search bounds the search space to find the minimum valid speed efficiently.
while lowest_possible_speed <= highest_possible_speed:
current_test_speed = (lowest_possible_speed + highest_possible_speed) // 2
hours_required = 0
for pile in banana_piles:
hours_required += math.ceil(pile / current_test_speed)
# If the speed allows finishing in time, record it and try to find an even slower speed.
if hours_required <= max_allowed_hours:
optimal_eating_speed = current_test_speed
highest_possible_speed = current_test_speed - 1
# If it takes too long, discard this speed and all slower speeds since they are invalid.
else:
lowest_possible_speed = current_test_speed + 1
return optimal_eating_speed| Case | How to Handle |
|---|---|
| Hours available h equals the number of piles | Binary search naturally sets speed k to the maximum pile size since Koko must eat each pile in one hour. |
| Hours available h is extremely large relative to the number of piles | Binary search efficiently shrinks the lower bound to a speed of 1 banana per hour. |
| Array contains a single pile with h equal to 1 | The algorithm correctly returns the exact size of that single pile as the minimum speed. |
| Array contains extremely large pile values up to 10 to the 9th power | Using 64-bit integers for the upper bound of the binary search prevents any potential overflow during calculation. |
| All piles have identical values | The ceiling division correctly aggregates the total hours required across all identical piles to determine valid speeds. |
| Pile values are heavily skewed with one massive pile and many tiny ones | The binary search range adapts instantly to the maximum pile value ensuring the upper bound covers the worst-case pile. |