有一些电池,每个电池可以使用batteries[i]时间,问要同时运行n台机器,每台机器需要一个电池,最多可以运行多久,二分
一开始r取小了点,WA*1
1 #2141
2 #Runtime: 2010 ms (Beats 33.33%)
3 #Memory: 25.1 MB (Beats 100%)
4
5 class Solution(object):
6 def maxRunTime(self, n, batteries):
7 """
8 :type n: int
9 :type batteries: List[int]
10 :rtype: int
11 """
12 l, r = 0, sum(batteries) // n + 1
13 while l <= r:
14 mid = (l + r) // 2
15 if sum(min(mid, b) for b in batteries) >= n * mid:
16 l = mid + 1
17 else:
18 r = mid - 1
19 return r