Jump Game (LeetCode 55): Solution and Explanation

Medium
Array
Dynamic Programming
Greedy

You start at the first index of an array where each value is the maximum jump length from that position. Decide whether some sequence of jumps can reach the last index.

Variables
No variables to display
DepthFunction Call
Stack empty
0/0

Intuition

A direct approach explores every possible jump, which branches heavily and takes exponential time without caching. With memoization it becomes a dynamic program: index i is good if any index in i + 1 through i + nums[i] is good, and the last index is good by definition. Filling that table from right to left is O(n²), because each index may scan a whole range of later positions.

Look closer at what that table is really asking. When you process index i from the right, you only need to know whether its reach includes some good index. And among all good indices to its right, the leftmost one is the easiest to reach. If i can reach the leftmost good index, it is good; if it cannot reach that one, it cannot reach any good index further right either, because those are even farther away. So you never need the whole table, just the position of the leftmost good index found so far.

That is the greedy solution. Keep a variable goal, starting at the last index. Walk backward. Whenever i + nums[i] >= goal, index i can reach the goal, so i becomes the new goal. At the end, if the goal has moved all the way to index 0, the start can reach the end. One backward pass, constant memory.

Approach: Greedy backward goal

  1. Set goal = len(nums) - 1, the last index, which trivially reaches itself.
  2. Loop i from len(nums) - 1 down to 0.
  3. If i + nums[i] >= goal, index i can jump to the current goal, so set goal = i.
  4. Otherwise leave goal unchanged; index i cannot reach any good position.
  5. After the loop, return goal == 0.

Step-by-step example

nums = [2, 3, 1, 1, 4]
  1. goal starts at 4. i = 4: 4 + 4 >= 4, so goal stays 4.
  2. i = 3: 3 + 1 = 4 >= 4, so goal = 3.
  3. i = 2: 2 + 1 = 3 >= 3, so goal = 2.
  4. i = 1: 1 + 3 = 4 >= 2, so goal = 1. i = 0: 0 + 2 = 2 >= 1, so goal = 0.
  5. goal is 0, so return True. For [3, 2, 1, 0, 4] the goal stays at 4, since 3 + 0, 2 + 1, 1 + 2 and 0 + 3 all equal 3, and the function returns False.

Code: Python, Java, C++

1class Solution:
2    def canJump(self, nums: List[int]) -> bool:
3        goal = len(nums) - 1
4        
5        for i in range(len(nums) - 1, -1, -1):
6            if i + nums[i] >= goal:
7                goal = i
8                
9        return goal == 0

Time and space complexity

Time

O(n)

The loop visits each index once and does a single comparison and possible assignment. Unlike the forward greedy, this version always scans the whole array, even when the answer is obvious early.

Space

O(1)

Only the goal variable and the loop index are stored.

Other approaches

Greedy forward max reach (time O(n), space O(1))

Walk left to right tracking the farthest index reachable so far. If you ever stand on an index beyond that reach, return false; if the reach covers the last index, return true. Same complexity, and it can stop early.

Bottom-up dynamic programming (time O(n²), space O(n))

Mark each index good if any position within its jump range is good, filling from the right. It is the natural stepping stone to the greedy insight but checks up to n positions per index.

Common mistakes

  • Treating nums[i] as the exact jump length instead of the maximum. You may jump any distance from 1 up to nums[i], which is why reaching or passing the goal is enough.
  • Using > instead of >= in the check. Landing exactly on the goal counts, so i + nums[i] == goal must move the goal.
  • Greedily always taking the longest jump forward. That can land on a zero and get stuck even when a shorter jump would have worked, as with [2, 5, 0, 0], where jumping 1 to the 5 reaches the end.
  • Returning True for an array with a zero somewhere without checking reach. A zero only blocks you if nothing before it can jump past it.

Frequently asked questions

Why does the greedy approach work for Jump Game?

Any index that can reach the leftmost known good index is itself good, and if it cannot reach that one it cannot reach any farther good index. Tracking only that leftmost good index loses no information.

What is the time complexity of Jump Game?

The greedy solution is O(n) time and O(1) space. The dynamic programming solution is O(n²) time and O(n) space.

Is a single-element array reachable?

Yes. You start on the last index, so goal is already 0 and the function returns true without needing any jumps.

How is Jump Game II different?

Jump Game II guarantees the end is reachable and asks for the minimum number of jumps. It is solved with a greedy, BFS-like sweep that tracks the farthest reach of each jump level.