|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: medium |
| 4 | +# Follow `Topics` tags |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Binary Search |
| 8 | +--- |
| 9 | + |
| 10 | +# [875. Koko Eating Bananas](https://leetcode.com/problems/koko-eating-bananas/description/) |
| 11 | + |
| 12 | +## Description |
| 13 | + |
| 14 | +Koko loves eating bananas. There are **n** piles of bananas, where the **i-th** pile contains `piles[i]` bananas. The guards are away and will return in **h** hours. |
| 15 | + |
| 16 | +Koko chooses an eating speed of **k** bananas per hour. Each hour, she selects one pile and eats up to **k** bananas from it. If the pile has fewer than **k** bananas, she eats all of them and does nothing else for the rest of that hour. |
| 17 | + |
| 18 | +Koko prefers to eat at the slowest possible speed, but she still wants to finish all the bananas before the guards return. |
| 19 | + |
| 20 | +Return the **minimum integer k** (bananas per hour) that allows her to finish all the bananas within **h** hours. |
| 21 | + |
| 22 | + |
| 23 | +**Example 1:** |
| 24 | +``` |
| 25 | +Input: piles = [30,11,23,4,20], h = 6 |
| 26 | +Output: 31 |
| 27 | +``` |
| 28 | + |
| 29 | +**Example 2:** |
| 30 | +``` |
| 31 | +Input: piles = [905306368,905306368,905306368], h = 1000000000 |
| 32 | +Output: 3 |
| 33 | +``` |
| 34 | + |
| 35 | +**Constraints:** |
| 36 | + |
| 37 | +* `1 <= piles.length <= 10^4` |
| 38 | +* `piles.length <= h <= 10^9` |
| 39 | +* `1 <= piles[i] <= 10^9` |
| 40 | + |
| 41 | +## Solution |
| 42 | + |
| 43 | + |
| 44 | +```java |
| 45 | +class Solution { |
| 46 | + public int minEatingSpeed(int[] piles, int h) { |
| 47 | + int min = 1, max = Arrays.stream(piles).max().getAsInt(); |
| 48 | + int ans = max; |
| 49 | + |
| 50 | + while(min <= max){ |
| 51 | + int mid = min + (max - min) / 2; |
| 52 | + long hours = 0; |
| 53 | + for(int pile : piles){ |
| 54 | + hours += (pile + mid - 1) / mid; |
| 55 | + } |
| 56 | + if(hours <= h){ |
| 57 | + max = mid - 1; |
| 58 | + ans = mid; |
| 59 | + } |
| 60 | + else{ |
| 61 | + min = mid + 1; |
| 62 | + } |
| 63 | + } |
| 64 | + return ans; |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +```python |
| 70 | +class Solution: |
| 71 | + def minEatingSpeed(self, piles: list[int], h: int) -> int: |
| 72 | + min_eating_speed, max_eating_speed = 1, max(piles) |
| 73 | + ans = max_eating_speed |
| 74 | + |
| 75 | + while min_eating_speed <= max_eating_speed: |
| 76 | + mid = min_eating_speed + (max_eating_speed - min_eating_speed) // 2 |
| 77 | + hours = 0 |
| 78 | + for pile in piles: |
| 79 | + hours += (pile + mid - 1) // mid |
| 80 | + if hours <= h: |
| 81 | + max_eating_speed = mid - 1 |
| 82 | + ans = mid |
| 83 | + else: |
| 84 | + min_eating_speed = mid + 1 |
| 85 | + return ans |
| 86 | +``` |
| 87 | + |
| 88 | +## Complexity |
| 89 | + |
| 90 | +- Time complexity: $$O(n log m)$$ |
| 91 | +<!-- Add time complexity here, e.g. $$O(n)$$ --> |
| 92 | + |
| 93 | +- Space complexity: $$O(1)$$ |
| 94 | +<!-- Add space complexity here, e.g. $$O(n)$$ --> |
| 95 | + |
0 commit comments