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
When 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:
The goal is to find the slowest eating speed Koko needs to eat all the bananas within a set time. The brute force method involves trying every possible eating speed, one by one, starting from the slowest possible, until we find the smallest speed that works.
Here's how the algorithm would work step-by-step:
def koko_eating_bananas_brute_force(piles, hours_allowed):
slowest_eating_speed = 1
while True:
total_hours_needed = 0
# Iterate through each pile of bananas
for pile in piles:
total_hours_needed += (pile + slowest_eating_speed - 1) // slowest_eating_speed
# Check if Koko can eat all bananas within the time limit
if total_hours_needed <= hours_allowed:
# If Koko can, then we return the eating speed, since we're checking from slowest to fastest
return slowest_eating_speed
# If Koko can't, increment the eating speed
slowest_eating_speed += 1
The core idea is to use a process of elimination to find the best eating speed for Koko. Instead of trying every possible speed, we narrow down the possibilities by checking if a speed is too slow or too fast, using a method similar to guessing a number between 1 and 100.
Here's how the algorithm would work step-by-step:
def koko_eating_bananas(piles, hours_to_eat): max_pile_size = max(piles)
left = 1
right = max_pile_size
while left < right:
eating_speed = (left + right) // 2
# Calculate how long it takes to eat all piles at this speed.
total_time_to_eat = 0
for pile_size in piles:
total_time_to_eat += (pile_size + eating_speed - 1) // eating_speed
# If it takes too long, the speed is too slow.
if total_time_to_eat > hours_to_eat:
left = eating_speed + 1
# The speed is fast enough, try a slower speed.
else:
right = eating_speed
# 'left' will be the minimum eating speed to finish in time.
return left
Case | How to Handle |
---|---|
piles is null or empty | Return 0 or throw an IllegalArgumentException as there are no bananas to eat, or return an impossible rate like Integer.MAX_VALUE indicating no solution. |
h is less than the number of piles | Return -1 or Integer.MAX_VALUE, indicating that it is impossible to eat all bananas within the given time, since Koko can only eat from one pile per hour. |
piles contains only one pile | Calculate the minimum k directly by dividing the number of bananas in the pile by h, ceiling the result, and return that. |
All piles have a small number of bananas (e.g., 1 or 2) | The binary search will quickly converge to a rate of 1 or 2, handled correctly by the `hoursNeeded` calculation. |
One pile has a huge amount of bananas (potential for integer overflow in calculation of hours) | Use long type for intermediate calculations of hours needed, especially when dividing pile size by rate. |
piles contains very large values leading to potential integer overflow when calculating hours | Ensure calculations for total hours use long data type to prevent overflow and provide accurate results. |
h is a very large number (e.g., greater than the sum of the pile sizes) | The binary search lower bound should be 1, and upper bound should be the max pile size; the binary search will converge to the smallest possible rate. |
All piles are the same size | The binary search will efficiently converge to the correct rate, as the `hoursNeeded` calculation will be consistent across iterations. |