Problem Description
Given an array stations where each element represents the number of power stations in a city, and an integer r representing the range of each station, each power station provides power to every city within distance r. The power of a city is the total number of stations that supply power to it. You are allowed to build k additional power stations at any cities. Your goal is to maximize the minimum power across all cities after optimally installing these additional stations.
Key Insights
- Use a sliding window (via prefix sum) to efficiently compute the initial power available at each city.
- Binary search over the possible minimum power values to decide the achievable power level.
- For each candidate power value, simulate the augmentation using a greedy approach combined with a difference array to add stations in the range where needed.
- Ensure the simulation updates the window contributions efficiently to maintain an overall O(n) check per candidate value.
Space and Time Complexity
Time Complexity: O(n * log(max_possible_power)), where n is the number of cities. Space Complexity: O(n) for the extra arrays (prefix/difference arrays).
Solution
The solution involves two main steps:
- Precompute the initial power for each city using a sliding window approach. This gives the sum of contributions from stations affecting that city.
- Use binary search to determine the maximum achievable minimum power. For each candidate minimum power, simulate placing additional stations greedily:
- Use a difference array to apply the effect of added stations quickly over the relevant range.
- If a city's power (initial power plus cumulative additions) falls short of the target, then add enough stations locally and propagate their effect using the difference array.
- If the total extra added stations exceed k, the candidate minimum power is infeasible; otherwise, it is feasible. Adjust the binary search range accordingly to find the maximum minimum power achievable.