Skip to content

Commit 48a4d97

Browse files
authored
Merge pull request #18 from xchux/feature/875-koko-eating-bananas
✨ (Solution): Problem 875. Koko Eating Bananas
2 parents cdfe482 + 9a6a4b6 commit 48a4d97

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int minEatingSpeed(int[] piles, int h) {
5+
int min = 1, max = Arrays.stream(piles).max().getAsInt();
6+
int ans = max;
7+
8+
while(min <= max){
9+
int mid = min + (max - min) / 2;
10+
long hours = 0;
11+
for(int pile : piles){
12+
hours += (pile + mid - 1) / mid;
13+
}
14+
if(hours <= h){
15+
max = mid - 1;
16+
ans = mid;
17+
}
18+
else{
19+
min = mid + 1;
20+
}
21+
}
22+
return ans;
23+
}
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def minEatingSpeed(self, piles: list[int], h: int) -> int:
3+
min_eating_speed, max_eating_speed = 1, max(piles)
4+
ans = max_eating_speed
5+
6+
while min_eating_speed <= max_eating_speed:
7+
mid = min_eating_speed + (max_eating_speed - min_eating_speed) // 2
8+
hours = 0
9+
for pile in piles:
10+
hours += (pile + mid - 1) // mid
11+
if hours <= h:
12+
max_eating_speed = mid - 1
13+
ans = mid
14+
else:
15+
min_eating_speed = mid + 1
16+
return ans

0 commit comments

Comments
 (0)