Path Existence Queries in a Graph II
Real-world version: Relay Hops Across The Field NetworkThe statement here is our own: the same inputs, outputs and technique.
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.
Examples
Example 1
- Input
- n = 3, level = [0, 1, 10], maxGap = 5, queries = [[0, 2], [0, 1], [2, 2]]
- Output
- [-1, 1, 0]
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
- Input
- n = 5, level = [0, 2, 4, 100, 102], maxGap = 2, queries = [[0, 2], [0, 4], [4, 3], [2, 0]]
- Output
- [2, -1, 1, 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
- Input
- n = 4, level = [7, 7, 7, 7], maxGap = 0, queries = [[0, 3], [1, 1]]
- Output
- [1, 0]
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.
Constraints
- 1 <= level.length <= 10^5
- n == level.length
- 0 <= level[i] <= 10^5
- 0 <= maxGap <= 10^5
- 1 <= queries.length <= 10^5
- queries[i].length == 2
- 0 <= queries[i][j] <= 10^5 - 1
- Every label listed in queries is the label of a relay in the network.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard 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)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Partition Array Into Two Arrays to Minimize Sum DifferenceReal-world version: Balanced Pontoon BallastHard
- Earliest Finish Time for Land and Water Rides IReal-world version: Forge and Kiln BookingEasy
- Earliest Finish Time for Land and Water Rides IIReal-world version: Pair of Workshop SlotsMedium
- Valid Triangle NumberReal-world version: Bracing Strut TriplesMedium