Skip to content

Commit ea055d5

Browse files
committed
Sync LeetCode submission Runtime - 1401 ms (40.49%), Memory - 30.5 MB (100.00%)
1 parent 89ab157 commit ea055d5

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<p>You have <code>n</code> computers. You are given the integer <code>n</code> and a <strong>0-indexed</strong> integer array <code>batteries</code> where the <code>i<sup>th</sup></code> battery can <strong>run</strong> a computer for <code>batteries[i]</code> minutes. You are interested in running <strong>all</strong> <code>n</code> computers <strong>simultaneously</strong> using the given batteries.</p>
2+
3+
<p>Initially, you can insert <strong>at most one battery</strong> into each computer. After that and at any integer time moment, you can remove a battery from a computer and insert another battery <strong>any number of times</strong>. The inserted battery can be a totally new battery or a battery from another computer. You may assume that the removing and inserting processes take no time.</p>
4+
5+
<p>Note that the batteries cannot be recharged.</p>
6+
7+
<p>Return <em>the <strong>maximum</strong> number of minutes you can run all the </em><code>n</code><em> computers simultaneously.</em></p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
<img alt="" src="https://assets.leetcode.com/uploads/2022/01/06/example1-fit.png" style="width: 762px; height: 150px;" />
12+
<pre>
13+
<strong>Input:</strong> n = 2, batteries = [3,3,3]
14+
<strong>Output:</strong> 4
15+
<strong>Explanation:</strong>
16+
Initially, insert battery 0 into the first computer and battery 1 into the second computer.
17+
After two minutes, remove battery 1 from the second computer and insert battery 2 instead. Note that battery 1 can still run for one minute.
18+
At the end of the third minute, battery 0 is drained, and you need to remove it from the first computer and insert battery 1 instead.
19+
By the end of the fourth minute, battery 1 is also drained, and the first computer is no longer running.
20+
We can run the two computers simultaneously for at most 4 minutes, so we return 4.
21+
22+
</pre>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
<img alt="" src="https://assets.leetcode.com/uploads/2022/01/06/example2.png" style="width: 629px; height: 150px;" />
26+
<pre>
27+
<strong>Input:</strong> n = 2, batteries = [1,1,1,1]
28+
<strong>Output:</strong> 2
29+
<strong>Explanation:</strong>
30+
Initially, insert battery 0 into the first computer and battery 2 into the second computer.
31+
After one minute, battery 0 and battery 2 are drained so you need to remove them and insert battery 1 into the first computer and battery 3 into the second computer.
32+
After another minute, battery 1 and battery 3 are also drained so the first and second computers are no longer running.
33+
We can run the two computers simultaneously for at most 2 minutes, so we return 2.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= n &lt;= batteries.length &lt;= 10<sup>5</sup></code></li>
41+
<li><code>1 &lt;= batteries[i] &lt;= 10<sup>9</sup></code></li>
42+
</ul>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Approach 2: Binary Search
2+
3+
class Solution:
4+
def maxRunTime(self, n: int, batteries: List[int]) -> int:
5+
left, right = 1, sum(batteries) // n
6+
7+
while left < right:
8+
target = right - (right - left) // 2
9+
10+
extra = 0
11+
for power in batteries:
12+
extra += min(power, target)
13+
14+
if extra // n >= target:
15+
left = target
16+
else:
17+
right = target - 1
18+
19+
return left
20+

0 commit comments

Comments
 (0)