Find the fewest forward hops to reach the last tower
A mountain rescue team places radio relay towers in a straight line along a trail. A message starts at the first tower and must reach the last one. Each tower has a limited range: tower i can pass the message forward to any tower from i + 1 up to i + ranges[i]. Every forward is one hop, and each hop adds delay, so the team wants the fewest hops.
Implement fewestHops(ranges). It returns the smallest number of hops that gets a message from tower 0 to the last tower, or -1 if the last tower cannot be reached.
fewestHops(ranges) // => number
ranges[i] is a non-negative integer. A range of 0 means that tower cannot forward the message at all.0.ranges has at least one tower and up to 20,000 towers. Trying every path is far too slow; aim for one pass.ranges.fewestHops([1, 3, 0, 2, 0, 1]); // => 3 (0 → 1 → 3 → 5)
fewestHops([2, 0, 2, 0, 1]); // => 2 (0 → 2 → 4)
fewestHops([4, 1, 1, 1, 1]); // => 1 (tower 0 reaches the end directly)
fewestHops([10, 0, 0]); // => 1
fewestHops([3, 2, 1, 0, 5]); // => -1 (every path stalls at tower 3)
fewestHops([0, 1]); // => -1
fewestHops([0]); // => 0