LeetCode: [2141] 同时运行 N 台电脑的最长时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
def maxRunTime(self, n, batteries):
left = 0
right = sum(batteries) // n
max_time = 0
while left <= right:
mid = (left + right) // 2
total = 0
for cap in batteries:
total += min(cap, mid)
if total >= mid * n:
max_time = mid
left = mid + 1
else:
right = mid - 1
return max_time