You are given a 0-indexed integer array stations of length n, where
stations[i] represents the number of power stations in the ith city.
Each power station can provide power to every city in a fixed range. In other words, if the range is denoted by r, then a power station at city i
can provide power to all cities j such that |i - j| <= r and 0 <= i, j <= n - 1.
Note that |x| denotes absolute value. For example, |7 - 5| = 2 and |3 - 10| = 7.
The power of a city is the total number of power stations it is being provided power from.
The government has sanctioned building k more power stations, each of which can be built in any city, and have the same range as the pre-existing ones.
Given the two integers r and k, return themaximum possible minimum power of a city, if the additional power stations are built optimally.
Note that you can build the k power stations in multiple cities.
Input: stations =[1,2,4,5,0], r =1, k =2Output: 5Explanation:
One of the optimal ways is to install both the power stations at city 1.So stations will become [1,4,4,5,0].- City 0is provided by 1+4=5 power stations.- City 1is provided by 1+4+4=9 power stations.- City 2is provided by 4+4+5=13 power stations.- City 3is provided by 5+4=9 power stations.- City 4is provided by 5+0=5 power stations.So the minimum power of a city is5.Since it is not possible to obtain a larger power, we return5.
To maximize the minimum power of any city, we can use binary search on the answer. For each candidate minimum, we check if it’s possible to achieve at least that much power in every city by optimally placing up to k new stations. We use a sliding window to efficiently track the current power in each city and greedily add stations where needed.
classSolution {
public:bool can(vector<int>& st, int r, longlong k, longlong minP) {
int n = st.size();
vector<longlong> a(st.begin(), st.end());
vector<longlong> diff(n+1, 0);
longlong add =0, sum =0;
for (int i =0; i < n; ++i) {
if (i >0) diff[i] += diff[i-1];
longlong cur = a[i] + diff[i];
if (cur < minP) {
longlong need = minP - cur;
if (need > k) return false;
k -= need;
int idx = min(n-1, i + r);
diff[i] += need;
if (idx+1< n) diff[idx+1] -= need;
}
}
return true;
}
longlongmaxPower(vector<int>& st, int r, int k) {
longlong l =0, rgt = accumulate(st.begin(), st.end(), 0LL) + k, ans =0;
while (l <= rgt) {
longlong m = (l + rgt) /2;
if (can(st, r, k, m)) { ans = m; l = m +1; }
else rgt = m -1;
}
return ans;
}
};
classSolution {
publiclongmaxPower(int[] st, int r, int k) {
long l = 0, h = 0;
for (int x : st) h += x;
h += k;
long ans = 0;
while (l <= h) {
long m = (l + h) / 2;
if (can(st, r, k, m)) { ans = m; l = m + 1; }
else h = m - 1;
}
return ans;
}
privatebooleancan(int[] st, int r, int k, long minP) {
int n = st.length;
long[] diff =newlong[n+1];
long add = 0;
for (int i = 0; i < n; i++) {
if (i > 0) diff[i]+= diff[i-1];
long cur = st[i]+ diff[i];
if (cur < minP) {
long need = minP - cur;
if (need > k - add) returnfalse;
add += need;
int idx = Math.min(n-1, i + r);
diff[i]+= need;
if (idx+1 < n) diff[idx+1]-= need;
}
}
returntrue;
}
}
classSolution {
funmaxPower(st: IntArray, r: Int, k: Int): Long {
var l = 0Lvar h = st.map { it.toLong() }.sum() + k
var ans = 0Lfuncan(minP: Long): Boolean {
val n = st.size
val diff = LongArray(n+1)
var add = 0Lfor (i in0 until n) {
if (i > 0) diff[i] += diff[i-1]
val cur = st[i] + diff[i]
if (cur < minP) {
val need = minP - cur
if (need > k - add) returnfalse add += need
val idx = minOf(n-1, i + r)
diff[i] += need
if (idx+1 < n) diff[idx+1] -= need
}
}
returntrue }
while (l <= h) {
val m = (l + h) / 2if (can(m)) { ans = m; l = m + 1 } else { h = m - 1 }
}
return ans
}
}
classSolution:
defmaxPower(self, stations: list[int], r: int, k: int) -> int:
n = len(stations)
defcan(x: int) -> bool:
diff = [0] * (n +1)
add =0for i in range(n):
if i >0:
diff[i] += diff[i-1]
cur = stations[i] + diff[i]
if cur < x:
need = x - cur
if need > k - add:
returnFalse add += need
idx = min(n-1, i + r)
diff[i] += need
if idx+1< n:
diff[idx+1] -= need
returnTrue l, h, ans =0, sum(stations) + k, 0while l <= h:
m = (l + h) //2if can(m):
ans = m
l = m +1else:
h = m -1return ans
impl Solution {
pubfnmax_power(stations: Vec<i64>, r: i64, k: i64) -> i64 {
let n = stations.len();
letmut l =0;
letmut h = stations.iter().sum::<i64>() + k;
letmut ans =0;
while l <= h {
let m = (l + h) /2;
letmut diff =vec![0; n+1];
letmut add =0;
letmut ok =true;
for i in0..n {
if i >0 { diff[i] += diff[i-1]; }
let cur = stations[i] + diff[i];
if cur < m {
let need = m - cur;
if need > k - add { ok =false; break; }
add += need;
let idx = (i asi64+ r).min(n asi64-1) asusize;
diff[i] += need;
if idx+1< n { diff[idx+1] -= need; }
}
}
if ok { ans = m; l = m +1; } else { h = m -1; }
}
ans
}
}