This article explores LeetCode problem 2528, “Maximize the Minimum Powered City,” a challenging problem that involves optimizing power distribution across cities. The core task is to determine the highest possible minimum power any city can achieve after strategically adding a limited number of new power stations.
Problem Overview:
You are provided with:
* An array stations where stations[i] denotes the existing number of power stations in city i.
* An integer r, representing the range of each power station. A station at city i provides power to all cities j such that |i - j| <= r.
* An integer k, the number of additional power stations that can be built. These new stations have the same range r.
The “power” of a city is the total count of power stations supplying it. The objective is to return the maximum possible minimum power across all cities if the k additional power stations are optimally placed.
Example Scenario (from problem description):
stations = [1,2,4,5,0], r = 1, k = 2
If two stations are added at city 1, stations becomes [1,4,4,5,0].
* City 0: Power from city 0 (1) + city 1 (4) = 5
* City 1: Power from city 0 (1) + city 1 (4) + city 2 (4) = 9
* City 2: Power from city 1 (4) + city 2 (4) + city 3 (5) = 13
* City 3: Power from city 2 (4) + city 3 (5) = 9
* City 4: Power from city 3 (5) + city 4 (0) = 5
The minimum power in this case is 5.
Solution Approach: Binary Search and Greedy Placement
This problem falls into the category of “maximize the minimum” problems, a strong indicator for employing binary search. We will binary search on the potential minimum power value that all cities can achieve. For each candidate minimum power, we need an efficient way to check if it’s feasible to reach that minimum using at most k additional stations.
- Initial Power Calculation:
Before considering new stations, we must calculate the current power for each city. Since a station atiaffects cities in[i-r, i+r], this is a range sum problem. A difference array (or “line sweep”) technique is highly efficient here. For each existing stationstations[i], we can mark an increment atmax(0, i-r)and a decrement atmin(n-1, i+r) + 1. A prefix sum pass over this difference array then yields the initial power for each city. -
Binary Search for Maximum Minimum Power:
The search space for our binary search will be from0(the absolute minimum power) up to a reasonable upper bound, which could be themaximum initial power + k. We’ll try to find the largesttargetvalue for whichisPossible(target)returns true. -
Feasibility Check (
isPossiblefunction):
Given atargetminimum power, this function determines if we can make all cities have at leasttargetpower using at mostkadditional stations. A greedy strategy is optimal here:- Iterate through each city from left to right.
- Maintain the current power for the city, accounting for initial stations and any newly added stations that affect it. A sliding window sum or another difference array can track this efficiently.
- If a city’s current power is less than
target, we must add stations. To be greedy and cover as many future cities as possible with the fewest new stations, we place the required number of stations at the rightmost possible position,current_city_index + r. This ensures the current city is covered and maximizes the range of subsequent cities that also benefit. - We track the total
kstations used. If at any point we exceedk, thetargetis not feasible. - If we successfully iterate through all cities and meet the
targetpower without exceedingk, thetargetis feasible.
Complexity Analysis:
- Time Complexity: O(N log(MaxPower + K)), where N is the number of cities. The initial power calculation takes O(N). The binary search performs O(log(MaxPower + K)) iterations. Each
isPossiblecheck takes O(N) time. - Space Complexity: O(N) for auxiliary arrays used in initial power calculation and the feasibility check.
This robust approach effectively tackles the “Maximize the Minimum Powered City” problem by combining the power of binary search for optimization with a greedy strategy for feasibility.