Fewest Hops to the Last Platform
A row of platforms is numbered from 0. Standing on platform i, a runner can hop forward to any platform from i + 1 up to i + reach[i].
The runner starts on platform 0. Return the fewest hops needed to land on the last platform. The input guarantees that the last platform is reachable.
Example 1
Input: reach = [3,1,1,2,1,5] Output: 2 Explanation: 0 → 3 → 5.
Example 2
Input: reach = [0] Output: 0 Explanation: Already on the last platform.
Constraints
- 1 <= reach.length <= 3 * 10^4
- 0 <= reach[i] <= 1000
- The last platform is reachable.
Hint 1
Think in rounds: all platforms reachable with h hops form a contiguous range.
Hint 2
Scan left to right, tracking the farthest platform reachable from the current range; when you pass the end of the range, spend one more hop.
Discussion 0
Join free to post in the discussion.
Good practice for: Game Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Embedded Systems Engineer
Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.