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.
|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.
Example 1:
Input: stations = [1,2,4,5,0], r = 1, k = 2 Output: 5 Explanation: One of the optimal ways is to install both the power stations at city 1. So stations will become [1,4,4,5,0]. - City 0 is provided by 1 + 4 = 5 power stations. - City 1 is provided by 1 + 4 + 4 = 9 power stations. - City 2 is provided by 4 + 4 + 5 = 13 power stations. - City 3 is provided by 5 + 4 = 9 power stations. - City 4 is provided by 5 + 0 = 5 power stations. So the minimum power of a city is 5. Since it is not possible to obtain a larger power, we return 5.
Example 2:
Input: stations = [4,4,4,4], r = 0, k = 3 Output: 4 Explanation: It can be proved that we cannot make the minimum power of a city greater than 4.
Constraints:
n == stations.length1 <= n <= 1050 <= stations[i] <= 1050 <= r <= n - 10 <= k <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a string of cities, each with a certain amount of power, and you want to make the weakest city as strong as possible by strategically adding new power stations. The brute force approach involves testing every single possible way to place the available power stations and calculating the resulting power for the weakest city in every scenario.
Here's how the algorithm would work step-by-step:
import itertools
def max_power_city_brute_force(initial_powers, range_radius, extra_stations):
num_cities = len(initial_powers)
maximum_minimum_power = -1
# Generate all possible combinations of city indices to place the extra power stations
for combination in itertools.combinations_with_replacement(range(num_cities), extra_stations):
current_powers = list(initial_powers)
for station_index in combination:
current_powers[station_index] += 1
effective_powers = [0] * num_cities
for city_index in range(num_cities):
start_range = max(0, city_index - range_radius)
end_range = min(num_cities - 1, city_index + range_radius)
for pointer in range(start_range, end_range + 1):
effective_powers[pointer] += current_powers[city_index]
# Find the minimum powered city for this specific station placement arrangement
current_minimum = min(effective_powers)
if current_minimum > maximum_minimum_power:
maximum_minimum_power = current_minimum
# Return the highest possible value among all the worst-case scenarios we tested
return maximum_minimum_powerTo find the best possible minimum power for any city, we can combine binary search with a greedy strategy. We guess a target power level, and then test if we can achieve it by strategically adding new power stations only where they help the most.
Here's how the algorithm would work step-by-step:
def max_power(stations, range_val, extra): n = len(stations) power = [0] * n current_sum = 0 for i in range(range_val): current_sum += stations[i] for i in range(n): if i + range_val < n: current_sum += stations[i + range_val] if i - range_val - 1 >= 0: current_sum -= stations[i - range_val - 1] power[i] = current_sum def check(target): added = [0] * n stations_added = 0 current_added_power = 0 window_sum = 0 for i in range(range_val): window_sum += added[i] for i in range(n): if i + range_val < n: window_sum += added[i + range_val] if i - range_val - 1 >= 0: window_sum -= added[i - range_val - 1] total_power = power[i] + current_added_power + window_sum if total_power < target: diff = target - total_power stations_added += diff if stations_added > extra: return False idx = min(n - 1, i + range_val) added[idx] += diff window_sum += diff current_added_power += diff return True low = 0 high = max(power) + extra answer = 0 # Binary search bounds the minimum power to maximize the worst-case city output efficiently. while low <= high: mid = (low + high) // 2 # We test if the current mid-point power target is achievable with available stations. if check(mid): answer = mid low = mid + 1 else: high = mid - 1 # Return the maximum possible minimum power level verified by our search strategy. return answer| Case | How to Handle |
|---|---|
| Large input values leading to integer overflow during power sums | Use 64-bit integer types for all power calculations and prefix sum arrays to prevent arithmetic overflow. |
| Range r is larger than or equal to the array length n | Clamp the effective range for each city to the actual boundaries of the array to avoid out-of-bounds errors. |
| K is large enough to make every city's power exceed any reasonable minimum threshold | Binary search will successfully scale up the target power until it finds the maximum possible lower bound. |
| All initial station powers are zero | The sliding window and greedy placement will correctly distribute the k stations to maximize the bottleneck power. |
| Minimum-sized array of length one with r equal to zero | The algorithm treats the single city independently and adds all k stations to its power. |
| Highly skewed distribution where one city has massive power and others have zero | The greedy check function will accurately identify the deficient cities and prioritize placing new stations there. |
| Maximum possible array size n up to 100,000 | Use a combination of binary search on the answer and a sliding window approach to ensure an efficient O(n log(sum)) time complexity. |