|
| 1 | +# Koko Eating Bananas |
| 2 | +Koko loves to eat bananas. There are n piles of bananas, and the i-th pile has piles[i] bananas. The guards have gone, and Koko has h hours to eat all the bananas. Each hour, she chooses a pile of bananas and eats k bananas from that pile. If the pile has fewer than k bananas, she eats all of them instead, and there will be no bananas left in that pile for the next hour. |
| 3 | + |
| 4 | +Return the minimum integer k such that Koko can eat all the bananas within h hours. |
| 5 | +### Constraints: |
| 6 | +- 1 <= piles.length <= 10^4 |
| 7 | +- piles.length <= h <= 10^9 |
| 8 | +- 1 <= piles[i] <= 10^9 |
| 9 | + |
| 10 | +### Examples |
| 11 | +```javascript |
| 12 | +Input: piles = [3,6,7,11], h = 8 |
| 13 | +Output: 4 |
| 14 | + |
| 15 | +Input: piles = [30,11,23,4,20], h = 5 |
| 16 | +Output: 30 |
| 17 | + |
| 18 | +Input: piles = [30,11,23,4,20], h = 6 |
| 19 | +Output: 23 |
| 20 | +``` |
| 21 | + |
| 22 | +## Approaches to Solve the Problem |
| 23 | +### Approach 1: Brute Force (Inefficient) |
| 24 | +##### Intuition: |
| 25 | +In a brute-force approach, we can iterate over all possible eating speeds k from 1 to the maximum number of bananas in the largest pile, checking for each speed if Koko can eat all the bananas in h hours. However, this approach is inefficient because it involves checking many values of k and can lead to a time complexity of O(n * max(piles)), where n is the number of piles. |
| 26 | + |
| 27 | +Steps: |
| 28 | +1. Iterate through possible values of k starting from 1. |
| 29 | +2. For each k, simulate the process to see if Koko can finish eating all piles within h hours. |
| 30 | +3. Return the smallest k that satisfies the condition. |
| 31 | +##### Time Complexity: |
| 32 | +O(n * max(piles)), where n is the number of piles and max(piles) is the largest pile size. |
| 33 | +##### Space Complexity: |
| 34 | +O(1), because we only use a few variables to track the possible values of k and time. |
| 35 | +##### Python Code: |
| 36 | +```python |
| 37 | +def can_eat_in_time(piles, h, k): |
| 38 | + hours = 0 |
| 39 | + for pile in piles: |
| 40 | + hours += (pile + k - 1) // k # Equivalent to ceil(pile / k) |
| 41 | + return hours <= h |
| 42 | + |
| 43 | +def minEatingSpeed(piles, h): |
| 44 | + for k in range(1, max(piles) + 1): |
| 45 | + if can_eat_in_time(piles, h, k): |
| 46 | + return k |
| 47 | +``` |
| 48 | +### Approach 2: Binary Search on Eating Speed (Optimal Solution) |
| 49 | +##### Intuition: |
| 50 | +We can use binary search to efficiently find the minimum possible eating speed k. The possible values for k range from 1 to the largest pile size in piles. The idea is to apply binary search over this range to minimize k while ensuring that Koko can finish all bananas within h hours. |
| 51 | + |
| 52 | +- Lower bound (low): The smallest possible speed is 1 banana per hour. |
| 53 | +- Upper bound (high): The maximum possible speed is the size of the largest pile (max(piles)), since Koko can finish the largest pile in one hour at this speed. |
| 54 | + |
| 55 | +Steps: |
| 56 | +1. Initialize low = 1 and high = max(piles). |
| 57 | +2. Perform binary search: |
| 58 | + - Calculate the middle point mid = (low + high) // 2. |
| 59 | + - Check if Koko can finish all the bananas at speed mid using the helper function can_eat_in_time(piles, h, mid). |
| 60 | + - If she can finish, update high = mid to try smaller values. |
| 61 | + - If she cannot finish, update low = mid + 1 to increase the eating speed. |
| 62 | +3. The binary search will converge, and low will be the minimum speed k. |
| 63 | +##### Time Complexity: |
| 64 | +O(n log max(piles)), where n is the number of piles, and log max(piles) comes from the binary search. |
| 65 | +##### Space Complexity: |
| 66 | +O(1), because we only use a few variables for binary search and time calculations. |
| 67 | +##### Python Code: |
| 68 | +```python |
| 69 | +def can_eat_in_time(piles, h, k): |
| 70 | + hours = 0 |
| 71 | + for pile in piles: |
| 72 | + hours += (pile + k - 1) // k # Equivalent to ceil(pile / k) |
| 73 | + return hours <= h |
| 74 | + |
| 75 | +def minEatingSpeed(piles, h): |
| 76 | + # Binary search on the possible values of k |
| 77 | + low, high = 1, max(piles) |
| 78 | + |
| 79 | + while low < high: |
| 80 | + mid = (low + high) // 2 |
| 81 | + if can_eat_in_time(piles, h, mid): |
| 82 | + high = mid # Try for smaller k |
| 83 | + else: |
| 84 | + low = mid + 1 # Increase k |
| 85 | + |
| 86 | + return low |
| 87 | +``` |
| 88 | +### Explanation of Code: |
| 89 | +- Binary Search: |
| 90 | +The binary search starts with a range of possible eating speeds from 1 to max(piles). We progressively narrow down this range by checking the midpoint (mid) using the can_eat_in_time function, which simulates the process of eating bananas at speed mid. |
| 91 | + |
| 92 | +- Helper Function can_eat_in_time(piles, h, k): |
| 93 | +This function calculates how many hours it would take Koko to finish eating all the piles if she eats k bananas per hour. For each pile, the time taken is ceil(pile / k), which is computed as (pile + k - 1) // k (this avoids using floating-point division). |
| 94 | + |
| 95 | +- Binary Search Condition: |
| 96 | +If Koko can finish eating all bananas at speed mid, we reduce the search space to find a potentially smaller value of k. Otherwise, we increase the speed and continue searching. |
| 97 | +### Edge Cases: |
| 98 | +1. Single Pile: If there is only one pile, the eating speed is equal to the size of the pile if h == 1. Otherwise, it will depend on how many hours Koko has. |
| 99 | +2. Many Small Piles, Few Hours: When there are many small piles but few hours, the binary search should quickly find the largest k needed to eat all piles in the limited time. |
| 100 | +3. Large Pile, Many Hours: If there's a very large pile and many hours, Koko can eat slowly, so the binary search should return a small k. |
| 101 | +## Summary |
| 102 | +| Approach | Time Complexity | Space Complexity | |
| 103 | +|-----------------------------------|-----------------|------------------| |
| 104 | +| Brute Force | O(n * max(piles)) | O(1) | |
| 105 | +| Binary Search (Optimal) | O(n log max(piles)) | O(1) | |
| 106 | + |
| 107 | +The Binary Search approach is the most efficient, reducing the time complexity to O(n log max(piles)) while using constant space. |
0 commit comments