Trains the technique from
LeetCode 3534Path Existence Queries in a Graph IIThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A survey team has planted n radio relays, labelled 0 through n - 1. Relay i is tuned to the frequency level[i].
Two relays can pass a message straight to each other exactly when the absolute difference between their frequencies is at most maxGap. That is the only thing that decides it: labels, distance on the ground and the order the relays were planted play no part, and every pair of relays whose frequencies are close enough can pass messages both ways.
Each query queries[t] = [a, b] asks for the smallest number of direct passes a message needs to travel from relay a to relay b. If no chain of passes joins them, the answer for that query is -1. When a and b are the same relay the answer is 0.
Return an array holding the answer to every query, in the order the queries are listed.
Example 1
Relays 0 and 1 are 1 apart, so they pass messages straight to each other. Relay 2 is 9 above relay 1 and 10 above relay 0, both beyond the gap, so nothing joins it to the rest. The last query starts and ends at the same relay.
Example 2
Relay 0 at 0 and relay 2 at 4 are 4 apart, too far for one pass, but relay 1 at 2 is within 2 of both, so a message can go 0, 1, 2. Relay 4 at 102 and relay 3 at 100 are 2 apart, so they pass directly, and no relay bridges the gap from 4 up to 100.
Example 3
All four relays share a frequency, so any two of them differ by 0, which a gap of 0 allows. A query between two different relays takes one pass, and one that starts where it ends takes none.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def relay_hop_counts(n: int, level: list[int], maxGap: int, queries: list[list[int]]) -> list[int]:public int[] relayHopCounts(int n, int[] level, int maxGap, int[][] queries)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.