Blind 75 · #62 · Greedy

Jump Game

MediumGreedy reachTime O(n)Space O(1)

Jump Game is a medium Greedy problem from the Blind 75. The key pattern is greedy reach, and a good solution runs in O(n) time.

Problem

Each value is the maximum forward jump length; determine whether the final index is reachable.

Examples

Example 1

Input

[3,2,1,0,4]

Output

false

Example 2

Input

[3,1,0,2,0,1]

Output

true

Example 3

Input

[0]

Output

true

Approach

Track the farthest index reachable so far. If you ever stand beyond it you are stuck; if it reaches the end you can finish.

PatternGreedy reach
TimeO(n)
SpaceO(1)

Watch out for

A 0 only blocks you when the reach cannot get past it.

More Greedy problems