File tree 1 file changed +12
-15
lines changed
scripts/algorithms/K/Koko Eating Bananas
1 file changed +12
-15
lines changed Original file line number Diff line number Diff line change 1
- import math
2
-
1
+ # Runtime: 308 ms (Top 92.4%) | Memory: 17.82 MB (Top 76.3%)
3
2
4
3
class Solution :
5
4
def minEatingSpeed (self , piles : List [int ], h : int ) -> int :
6
- n = len (piles )
7
- if n == h :
8
- return max (piles )
9
- piles .sort (reverse = True )
10
- extra = h - n # each pile takes at least 1 hour
11
- total_amount = 0
12
- for i , pile in enumerate (piles ):
13
- if extra < i + 1 :
14
- return pile # 1 hour lowest resolution
5
+ def check (x ):
6
+ return sum (ceil (ele / x ) for ele in piles ) <= h
15
7
16
- total_amount += pile
17
- tmp_k = math .ceil (total_amount / (extra + i + 1 ))
18
- if i + 1 < n and tmp_k > piles [i + 1 ] or i + 1 == n :
19
- return tmp_k # round up
8
+ l = 1
9
+ r = max (piles )
10
+ while l < r :
11
+ mid = (l + r ) >> 1
12
+ if not check (mid ):
13
+ l = mid + 1
14
+ else :
15
+ r = mid
16
+ return l
You can’t perform that action at this time.
0 commit comments