-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path875. Koko Eating Bananas.py
More file actions
36 lines (30 loc) · 964 Bytes
/
875. Koko Eating Bananas.py
File metadata and controls
36 lines (30 loc) · 964 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
31
32
33
34
35
36
from typing import List
import math
def minEatingSpeed(piles: List[int], h: int) -> int:
def findTimeNeeded(piles: List[int], speed: int) -> int:
if speed == 0:
return math.inf
ret = 0
for pile in piles:
ret += math.ceil(pile / speed)
return ret
l, r = 1, max(piles) # speed in banana per hour
while l < r: # find the minimum such that time needed < h
m = l + (r - l) // 2
time = findTimeNeeded(piles, m) # searching the lower upper bound
if time <= h:
r = m
else:
l = m + 1
print(l)
return l
# m = l + (r - l) // 2
# time = findTimeNeeded(piles, m) # searching the lower upper bound
# if time <= h:
# r = m
# else:
# l = m + 1
# return False
minEatingSpeed([1, 1, 1, 999999999], 10) # 142857143
minEatingSpeed([30, 11, 23, 4, 20], 5) # 30
minEatingSpeed([312884470], 312884469) # 2