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 City INPUT Cities with Power Stations C0 1 C1 2 C2 4 C3 5 C4 0 Range r=1 coverage example: Station at i covers i-1 to i+1 Input Values stations = [1,2,4,5,0] r = 1 (range) k = 3 (new stations) Initial power per city: [3, 7, 11, 9, 5] (sum of stations in range) ALGORITHM STEPS 1 Binary Search Setup Search min power: [0, sum+k] lo=0, hi=15 2 Greedy Check For mid value, check if achievable with k stations 3 Place Stations Greedily Left to right, add at i+r to maximize coverage 4 Narrow Search If feasible: lo = mid + 1 Else: hi = mid - 1 Binary Search Progress mid=7: need 4 --> NO mid=3: need 0 --> OK mid=5: need 3 --> OK mid=6: need 4 --> NO Answer = 5 FINAL RESULT Optimal Station Placement C0 1 +0 C1 2 +0 C2 4 +0 C3 5 +3 C4 0 +0 Final Power per City: 6 10 14 12 5 Min power = 5 (at City 4) OUTPUT 5 Maximum minimum power Key Insight: Binary search on the answer! Instead of directly computing optimal placement, we ask: "Can we achieve minimum power M with k stations?" Using greedy placement (place at rightmost position in range), we can check feasibility in O(n) time. TutorialsPoint - Maximize the Minimum Powered City | Greedy + Binary Search
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