2528. Maximize the Minimum Powered City
Description
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| = 2and|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 the maximum 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.
Example 1:
Input: stations = [1,2,4,5,0], r = 1, k = 2 Output: 5 Explanation: 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 0 is provided by 1 + 4 = 5 power stations. - City 1 is provided by 1 + 4 + 4 = 9 power stations. - City 2 is provided by 4 + 4 + 5 = 13 power stations. - City 3 is provided by 5 + 4 = 9 power stations. - City 4 is provided by 5 + 0 = 5 power stations. So the minimum power of a city is 5. Since it is not possible to obtain a larger power, we return 5.
Example 2:
Input: stations = [4,4,4,4], r = 0, k = 3 Output: 4 Explanation: It can be proved that we cannot make the minimum power of a city greater than 4.
Constraints:
n == stations.length1 <= n <= 1050 <= stations[i] <= 1050 <= r <= n - 10 <= k <= 109
Solutions
Solution: Binary Search + Sliding Window + Difference Array
- Time complexity: O(nlog(sum(stations)+k))
- Space complexity: O(n)
JavaScript
/**
* @param {number[]} stations
* @param {number} r
* @param {number} k
* @return {number}
*/
const maxPower = function (stations, r, k) {
const n = stations.length;
const prefixSum = Array.from({ length: n + 1 }, () => 0);
let left = Math.min(...stations);
let right = stations.reduce((a, b) => a + b, 0) + k;
for (let index = 1; index <= n; index++) {
prefixSum[index] = prefixSum[index - 1] + stations[index - 1];
}
const isValidPower = target => {
const diffs = new Array(n + 2).fill(0);
let remainStations = k;
for (let index = 1; index <= n; index++) {
diffs[index] += diffs[index - 1];
const left = Math.max(0, index - r - 1);
const right = Math.min(n, index + r);
const powers = prefixSum[right] - prefixSum[left] + diffs[index];
if (powers < target) {
const needStations = target - powers;
if (needStations > remainStations) return false;
const edge = Math.min(n + 1, index + r * 2 + 1);
remainStations -= needStations;
diffs[index] += needStations;
diffs[edge] -= needStations;
}
}
return true;
};
while (left <= right) {
const mid = Math.floor((left + right) / 2);
isValidPower(mid) ? (left = mid + 1) : (right = mid - 1);
}
return right;
};