mediumDynamic ProgrammingDynamic Programming

Jump Game

mediumTime: O(n)Space: O(1)

Recognize the pattern

can you reach the last indexjump at most nums[i] stepsreachability check

Brute force idea

If you approach Jump Game in the most literal way possible, you get this: BFS/DFS from index 0, trying all jump distances — in the worst case due to redundant state exploration. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.

Better approach

A calmer way to see Jump Game is this: Greedy: maintain the farthest index you can reach. Scan left to right — if the current index exceeds your max reach, you're stuck. If max reach ≥ last index, you can make it. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

Key invariant

The truth you want to protect throughout Jump Game is this: maxReach = max(maxReach, i + nums[i]) at each step. If i > maxReach at any point, position i is unreachable and so is everything after it. If that remains true after every update, the rest of the reasoning has a stable place to stand.

Watch out for

The trap in Jump Game usually looks like this: Trying to track the actual jump path — you don't need it. Only the farthest reachable index matters for the yes/no answer. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.

Dynamic Programming Pattern