Maximize the Minimum Powered City - Problem

Imagine you're an urban planner tasked with optimizing the power grid distribution across n cities arranged in a line. Each city currently has some number of power stations, given in the array stations[i].

Here's the key: each power station has a fixed range r, meaning a station in city i can provide power to all cities j where |i - j| <= r. The power of a city is the total number of stations that can reach it.

The government has approved funding for k additional power stations that you can place strategically in any cities. Your goal is to maximize the minimum power across all cities - essentially, you want to ensure even the least-powered city has as much power as possible.

Example: If stations = [1,2,4,5,0], r = 1, and k = 2, you need to determine the optimal placement of 2 new stations to maximize the minimum power any city receives.

Input & Output

example_1.py โ€” Basic Case
$ Input: stations = [1,2,4,5,0], r = 1, k = 2
โ€บ Output: 5
๐Ÿ’ก Note: Initially: City powers are [4,8,11,9,5] (each city gets power from stations within range r=1). The minimum is 4. We can place 2 additional stations optimally to achieve minimum power of 5.
example_2.py โ€” Small Range
$ Input: stations = [4,4,4,4], r = 0, k = 3
โ€บ Output: 4
๐Ÿ’ก Note: With r=0, each station only powers its own city. All cities start with power 4. Even with 3 additional stations, we can't increase the minimum beyond 4 by optimal placement.
example_3.py โ€” Large Range
$ Input: stations = [1,1], r = 2, k = 10
โ€บ Output: 11
๐Ÿ’ก Note: With r=2, both cities receive power from all stations. Initially both have power 2. With 10 additional stations placed optimally, both cities can reach power 11.

Visualization

Tap to expand
Power Grid Optimization StrategyStep 1: Calculate Initial City Powers (Sliding Window)P=4P=8P=11P=9P=5Min Power = 4Step 2: Binary Search on Minimum PowerLeft = 0Right = 17Mid = 8Step 3: Greedy Validation (Can we achieve min power = 8?)4<88โ‰ฅ811โ‰ฅ89โ‰ฅ85<8+4 stations+3 stationsGreedy Strategy Keyโ€ข Place stations RIGHTMOST in rangeโ€ข Maximizes coverage for future citiesโ€ข Minimizes total stations neededResult: Min Power = 5Total stations used: 7 โ‰ค k=2? NONeed to search lower valuesBinary search continues...
Understanding the Visualization
1
Calculate Initial Power
Use sliding window to compute each city's power from existing stations
2
Binary Search Setup
Search space from 0 to sum(stations) + k for maximum achievable minimum
3
Greedy Validation
For each candidate minimum, greedily place stations as far right as possible
4
Optimal Placement
Rightmost placement maximizes coverage for future cities
Key Takeaway
๐ŸŽฏ Key Insight: Binary search on the answer combined with greedy rightmost placement creates an optimal O(n log(sum+k)) solution that efficiently handles large inputs.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log(sum + k))

Binary search runs log(sum+k) iterations, each validation takes O(n) time with sliding window

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for storing current power configuration and additional stations placed

n
2n
โšก Linearithmic Space

Constraints

  • n == stations.length
  • 1 โ‰ค n โ‰ค 105
  • 0 โ‰ค stations[i] โ‰ค 105
  • 0 โ‰ค r โ‰ค n - 1
  • 0 โ‰ค k โ‰ค 109
  • Large k values require efficient O(n log n) solution
Asked in
Google 12 Meta 8 Amazon 6 Microsoft 4
21.1K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen