Minimum Relay Power for Full Network Coverage
Heaps, Sorting & SearchAuto-graded · 17 test casesLast updated
Tests binary search on the answer combined with a greedy coverage check to find the minimum broadcast power so every city is reachable from a central hub.
Minimum Relay Power for Full Network Coverage
You are deploying a linear network of relay stations along a single road. The stations are placed at integer positions on a number line. One station is designated the hub (the starting point of the broadcast). The hub transmits a signal that can reach any station within distance power of it. Every station that receives the signal also re-transmits it, extending coverage by the same power in either direction.
Formally, the reachable set grows as follows:
- Start with the hub position.
- Repeatedly add any station whose position is within
powerof any already-reachable station. - Repeat until no new station can be added.
You need all stations to be reachable from the hub.
Given the list of station positions stations and the index hub_index (0-based index into stations), find the minimum non-negative integer power such that every station in stations is reachable from the hub.
How the greedy coverage check works
Sort the stations by position. Starting from the hub, greedily extend coverage: from the current rightmost reachable position, jump to the farthest station still within power, then repeat. Do the same leftward. If at any point there is a gap larger than power between consecutive stations (in either direction from the hub), not all stations are reachable.
Constraints
1 ≤ len(stations) ≤ 2000-10^6 ≤ stations[i] ≤ 10^6- All station positions are distinct.
0 ≤ hub_index < len(stations)- The answer is guaranteed to fit in a 32-bit signed integer.
- If there is only one station (the hub), the answer is
0.
Examples
Example 1
Input: min_relay_power([0, 10, 20, 30, 45], 0)
Output: 15
Example 2
Input: min_relay_power([5, 1, 9, 3, 7], 2)
Output: 2
Example 3
Input: min_relay_power([42], 0)
Output: 0
Hints
Hint 1
Sort the stations by position first — the coverage check becomes a simple linear scan for gaps between consecutive stations.
Hint 2
Binary search on the answer: the feasibility function is monotone (if power p works, so does p+1), so binary the search space [0, max_gap_between_consecutive_sorted_stations].
Hint 3
For the coverage check, find the hub in the sorted order, then scan left and right: if any consecutive gap exceeds the candidate power, that power is insufficient.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight
The problem has monotone feasibility: if power p allows full coverage, then any power p' > p also works. This makes binary search on the answer applicable.
Algorithm
-
Sort the station positions. Connectivity from the hub depends entirely on the gaps between consecutive sorted stations — if every gap from the hub outward (in both directions) is at most
power, the chain of relays can carry the signal end-to-end. -
Binary search over
[0, max_consecutive_gap]:- The lower bound is
0(single station or all co-located). - The upper bound is the largest gap between any two consecutive stations in sorted order — no answer can exceed this since we need to bridge that gap.
- The lower bound is
-
Feasibility check for a candidate
power:- Locate the hub in the sorted array.
- Scan right: if any gap
sorted[i+1] - sorted[i] > power, the chain breaks. - Scan left: if any gap
sorted[i] - sorted[i-1] > power, the chain breaks. - If neither side breaks, return
True.
Why It's Correct
A relay chain can propagate the signal step-by-step through sorted stations as long as no consecutive pair is farther apart than power. Because stations are on a line and the hub is somewhere in the middle, we need the chain to be unbroken in both directions. The greedy scan directly checks this in O(n).
Complexity
- Sorting: O(n log n)
- Binary search: O(log(max_gap)) iterations, each O(n) → O(n log(max_gap))
- Overall: O(n log n + n log(max_gap)) — effectively O(n log(V)) where V = 2×10^6
- Space: O(n) for the sorted copy
from typing import List
def min_relay_power(stations: List[int], hub_index: int) -> int:
if len(stations) == 1:
return 0
hub_pos = stations[hub_index]
sorted_stations = sorted(stations)
# Maximum possible answer: the largest gap between consecutive sorted stations
max_gap = 0
for i in range(1, len(sorted_stations)):
max_gap = max(max_gap, sorted_stations[i] - sorted_stations[i - 1])
def can_cover(power: int) -> bool:
hub_sorted_idx = sorted_stations.index(hub_pos)
# Check right side
for i in range(hub_sorted_idx, len(sorted_stations) - 1):
if sorted_stations[i + 1] - sorted_stations[i] > power:
return False
# Check left side
for i in range(hub_sorted_idx, 0, -1):
if sorted_stations[i] - sorted_stations[i - 1] > power:
return False
return True
lo, hi = 0, max_gap
while lo < hi:
mid = (lo + hi) // 2
if can_cover(mid):
hi = mid
else:
lo = mid + 1
return lo