Maximize the Minimum Powered City - Problem

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: |x| denotes absolute value. For example, |7 - 5| = 2 and |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.

Input & Output

Example 1 — Basic Case
$ Input: stations = [1,2,4,5,0], r = 1, k = 3
Output: 5
💡 Note: Initial powers: [3,7,11,9,5]. With k=3 stations optimally placed, we can achieve minimum power of 5 across all cities.
Example 2 — 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. We have 3 additional stations to distribute, but can't improve minimum power beyond 4.
Example 3 — Large Range
$ Input: stations = [1,2,4,5,6], r = 2, k = 5
Output: 11
💡 Note: With r=2, each station covers 5 cities. Strategic placement of 5 additional stations can achieve minimum power of 11.

Constraints

  • n == stations.length
  • 1 ≤ n ≤ 105
  • 0 ≤ stations[i] ≤ 105
  • 0 ≤ r ≤ n - 1
  • 0 ≤ k ≤ 109

Visualization

Tap to expand
Maximize the Minimum Powered City12450City 0City 1City 2City 3City 4Range r=1: Each station covers adjacent citiesInitial Powers: [3, 7, 11, 9, 5]Goal: Place k=3 additional stations optimallyOutput: Maximum possible minimum power = 5Strategy: Binary search + greedy placement
Understanding the Visualization
1
Input
Cities with stations: [1,2,4,5,0], range r=1, additional k=3
2
Coverage
Each station covers cities within distance r
3
Optimization
Place k stations to maximize minimum city power
Key Takeaway
🎯 Key Insight: Binary search the minimum power value, then greedily verify if achievable by placing stations as far right as possible within range
Asked in
Google 15 Amazon 12 Microsoft 8
15.2K Views
Medium Frequency
~35 min Avg. Time
487 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