-
Notifications
You must be signed in to change notification settings - Fork 31
/
0875-koko-eating-bananas.py
28 lines (20 loc) · 1.16 KB
/
0875-koko-eating-bananas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""
Problem: LeetCode 875 - Koko Eating Bananas
Key Idea:
The key idea is to perform binary search to find the minimum value of the integer 'k' such that Koko can eat all the bananas within 'hours' hours. We can define a binary search space for 'k' and perform binary search to find the smallest 'k' that satisfies the given condition.
Time Complexity:
The time complexity of this solution is O(n * log(max_pile)), where n is the number of piles and max_pile is the maximum size of a pile. The binary search iterates log(max_pile) times, and for each iteration, we perform a linear search through 'piles' to calculate the total hours.
Space Complexity:
The space complexity is O(1), as no extra space is used other than a few variables to keep track of indices and values.
"""
class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
left, right = 1, max(piles)
while left < right:
mid = left + (right - left) // 2
hours = sum((pile + mid - 1) // mid for pile in piles)
if hours > h:
left = mid + 1
else:
right = mid
return left