BugForge
DSA

Fewest Hops Across Relay Towers

MediumgreedyBFS levels 00:00

range[i] is how many towers ahead tower i can relay to. Starting at tower 0, return the minimum hops to reach the last tower. It's guaranteed reachable.

Signature

minHops(reach: int[]) → int
Example 1
reach = [2,3,1,1,4]
output = 2
0 → 1 → 4: 2 hops
Example 2
reach = [2,3,0,1,4]
output = 2

Constraints

  • 1 ≤ n ≤ 10^4
  • 0 ≤ range[i] ≤ 1000
  • last tower reachable
Run checks the examples. Submit runs every hidden test (17, including large inputs that catch slow solutions).