2528. Maximize the Minimum Powered City #2391
-
|
Topics: You are given a 0-indexed integer array Each power station can provide power to every city in a fixed range. In other words, if the range is denoted by
The power of a city is the total number of power stations it is being provided power from. The government has sanctioned building Given the two integers Note that you can build the Example 1:
Example 2:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
We need to find the maximum possible minimum power across all cities after optimally placing k additional power stations. Approach:
Let's implement this solution in PHP: 2528. Maximize the Minimum Powered City <?php
/**
* @param Integer[] $stations
* @param Integer $r
* @param Integer $k
* @return Integer
*/
function maxPower($stations, $r, $k) {
$n = count($stations);
// Step 1: Calculate initial power using prefix sum
$prefix = array_fill(0, $n + 1, 0);
for ($i = 0; $i < $n; $i++) {
$left = max(0, $i - $r);
$right = min($n - 1, $i + $r);
$prefix[$left] += $stations[$i];
$prefix[$right + 1] -= $stations[$i];
}
$power = array_fill(0, $n, 0);
$power[0] = $prefix[0];
for ($i = 1; $i < $n; $i++) {
$power[$i] = $power[$i - 1] + $prefix[$i];
}
// Step 2: Binary search for maximum minimum power
$left = 0;
$right = max($power) + $k;
$answer = 0;
while ($left <= $right) {
$mid = (int)(($left + $right) / 2);
if (isPossible($power, $r, $k, $mid)) {
$answer = $mid;
$left = $mid + 1;
} else {
$right = $mid - 1;
}
}
return $answer;
}
/**
* @param $initialPower
* @param $r
* @param $k
* @param $target
* @return bool
*/
function isPossible($initialPower, $r, $k, $target) {
$n = count($initialPower);
$power = $initialPower;
$add = array_fill(0, $n + 1, 0);
$currAdd = 0;
$stationsUsed = 0;
for ($i = 0; $i < $n; $i++) {
// Remove additions that are out of range
if ($i > $r) {
$currAdd -= $add[$i - $r - 1];
}
$currentPower = $power[$i] + $currAdd;
if ($currentPower < $target) {
$needed = $target - $currentPower;
if ($needed > $k - $stationsUsed) {
return false;
}
$pos = min($n - 1, $i + $r);
$add[$pos] += $needed;
$currAdd += $needed;
$stationsUsed += $needed;
}
}
return true;
}
// Test cases
echo maxPower([1, 2, 4, 5, 0], 1, 2) . PHP_EOL; // Output: 5
echo maxPower([4, 4, 4, 4], 0, 3) . PHP_EOL; // Output: 4
?>Explanation:
Time Complexity: O(n log(max_power + k)), where n is the number of cities |
Beta Was this translation helpful? Give feedback.
We need to find the maximum possible minimum power across all cities after optimally placing k additional power stations.
Approach:
Understanding the power calculation: Each power station at position
iprovides power to all cities within[i-r, i+r]. So the power of a city is the sum of all stations that can reach it.Key insight: This is a classic "maximize the minimum" problem, which suggests using binary search. I'll binary search on the possible minimum power values.
Preprocessing: First, I need to calculate the initial power for each city. I can use a prefix sum/difference array technique to efficiently compute how many stations cover each city.
Checking feasibility: For a can…