Skip to content

Commit f4760a1

Browse files
committed
[LeetCode Sync] Runtime - 1476 ms (60.83%), Memory - 33.3 MB (41.24%)
1 parent c917f87 commit f4760a1

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<p>You are given a <strong>0-indexed</strong> integer array <code>stations</code> of length <code>n</code>, where <code>stations[i]</code> represents the number of power stations in the <code>i<sup>th</sup></code> city.</p>
2+
3+
<p>Each power station can provide power to every city in a fixed <strong>range</strong>. In other words, if the range is denoted by <code>r</code>, then a power station at city <code>i</code> can provide power to all cities <code>j</code> such that <code>|i - j| &lt;= r</code> and <code>0 &lt;= i, j &lt;= n - 1</code>.</p>
4+
5+
<ul>
6+
<li>Note that <code>|x|</code> denotes <strong>absolute</strong> value. For example, <code>|7 - 5| = 2</code> and <code>|3 - 10| = 7</code>.</li>
7+
</ul>
8+
9+
<p>The <strong>power</strong> of a city is the total number of power stations it is being provided power from.</p>
10+
11+
<p>The government has sanctioned building <code>k</code> more power stations, each of which can be built in any city, and have the same range as the pre-existing ones.</p>
12+
13+
<p>Given the two integers <code>r</code> and <code>k</code>, return <em>the <strong>maximum possible minimum power</strong> of a city, if the additional power stations are built optimally.</em></p>
14+
15+
<p><strong>Note</strong> that you can build the <code>k</code> power stations in multiple cities.</p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong class="example">Example 1:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> stations = [1,2,4,5,0], r = 1, k = 2
22+
<strong>Output:</strong> 5
23+
<strong>Explanation:</strong>
24+
One of the optimal ways is to install both the power stations at city 1.
25+
So stations will become [1,4,4,5,0].
26+
- City 0 is provided by 1 + 4 = 5 power stations.
27+
- City 1 is provided by 1 + 4 + 4 = 9 power stations.
28+
- City 2 is provided by 4 + 4 + 5 = 13 power stations.
29+
- City 3 is provided by 5 + 4 = 9 power stations.
30+
- City 4 is provided by 5 + 0 = 5 power stations.
31+
So the minimum power of a city is 5.
32+
Since it is not possible to obtain a larger power, we return 5.
33+
</pre>
34+
35+
<p><strong class="example">Example 2:</strong></p>
36+
37+
<pre>
38+
<strong>Input:</strong> stations = [4,4,4,4], r = 0, k = 3
39+
<strong>Output:</strong> 4
40+
<strong>Explanation:</strong>
41+
It can be proved that we cannot make the minimum power of a city greater than 4.
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>n == stations.length</code></li>
49+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
50+
<li><code>0 &lt;= stations[i] &lt;= 10<sup>5</sup></code></li>
51+
<li><code>0 &lt;= r&nbsp;&lt;= n - 1</code></li>
52+
<li><code>0 &lt;= k&nbsp;&lt;= 10<sup>9</sup></code></li>
53+
</ul>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution:
2+
def maxPower(self, stations: List[int], r: int, k: int) -> int:
3+
n = len(stations)
4+
f = [0] * (n + 1)
5+
for i, val in enumerate(stations):
6+
left, right = max(0, i - r), min(i + r, n - 1)
7+
f[left] += val
8+
f[right + 1] -= val
9+
10+
s = list(accumulate(f))
11+
12+
def checker(x, k):
13+
f = [0] * (n + 1)
14+
t = 0
15+
for i in range(n):
16+
t += f[i]
17+
dist = x - (s[i] + t)
18+
if dist > 0:
19+
if k < dist:
20+
return False
21+
k -= dist
22+
j = min(i + r, n - 1)
23+
left, right = max(0, j - r), min(j + r, n - 1)
24+
f[left] += dist
25+
f[right + 1] -= dist
26+
t += dist
27+
return True
28+
29+
left, right = 0, 1 << 40
30+
while right > left:
31+
mid = (left + right + 1) // 2
32+
if checker(mid, k):
33+
left = mid
34+
else:
35+
right = mid - 1
36+
return left

0 commit comments

Comments
 (0)