-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0875-koko-eating-bananas.py
More file actions
30 lines (26 loc) · 743 Bytes
/
0875-koko-eating-bananas.py
File metadata and controls
30 lines (26 loc) · 743 Bytes
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
29
30
# time complexity: O(nlogm)
# space complexity: O(1)
import math
from typing import List
class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
left, right = 1, max(piles)
while left < right:
mid = left + (right - left) // 2
hourSpent = 0
for pile in piles:
hourSpent += math.ceil(pile / mid)
if hourSpent <= h:
right = mid
else:
left = mid + 1
return right
piles = [3, 6, 7, 11]
h = 8
print(Solution().minEatingSpeed(piles, h))
piles = [30, 11, 23, 4, 20]
h = 5
print(Solution().minEatingSpeed(piles, h))
piles = [30, 11, 23, 4, 20]
h = 6
print(Solution().minEatingSpeed(piles, h))