Loading
Loading
A platform game level is a row of platforms. You start on platform 0 and the exit is the last platform. Each platform has a spring with a strength: from platform i you can hop forward to any platform at most reach[i] positions ahead. A spring of strength 0 cannot launch you anywhere.
Return the fewest hops needed to land on the exit, or -1 if the exit cannot be reached.
reach = [2, 3, 1, 1, 4]0 hops: platforms 0..0the start1 hop: platforms 1..2platform 0 reaches up to 0 + 22 hops: platforms 3..4platform 1 reaches up to 1 + 3, which includes the exit2Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n) time · O(1) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
Hop from platform 0 to 1, then use its strength of 3 to land on the exit.
Example 2
Every route lands on platform 3, whose spring has strength 0.
Example 3
You start on the exit, so no hops are needed.