Skip to content

Commit ef64e41

Browse files
authored
Update maximum-running-time-of-n-computers.py
1 parent 928e285 commit ef64e41

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Python/maximum-running-time-of-n-computers.py

+23
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,26 @@ def maxRunTime(self, n, batteries):
2020
n -= 1
2121
total -= -heapq.heappop(batteries)
2222
return total//n
23+
24+
25+
# Time: O(nlogr), r is the range of possible minutes
26+
# Space: O(1)
27+
# binary search
28+
class Solution2(object):
29+
def maxRunTime(self, n, batteries):
30+
"""
31+
:type n: int
32+
:type batteries: List[int]
33+
:rtype: int
34+
"""
35+
def check(n, batteries, x):
36+
return sum(min(b, x) for b in batteries) >= n*x
37+
38+
left, right = min(batteries), sum(batteries)//n
39+
while left <= right:
40+
mid = left + (right-left)//2
41+
if not check(n, batteries, mid):
42+
right = mid-1
43+
else:
44+
left = mid+1
45+
return right

0 commit comments

Comments
 (0)