LeetCode 55 · Medium

Jump Game Explained (LeetCode 55)

Updated Dec 4, 2026 · 3 min read

Keep one number, farthest, the greatest index you can reach so far. Walk the array left to right: if the current index i is past farthest, you can never get there, so return False; otherwise set farthest = max(farthest, i + nums[i]). If the loop finishes, return True. That's O(n) time and O(1) space.

Jump Game reel cover

Key points

  • ●Always taking the biggest jump is wrong: on [2, 5, 0, 0, 1] it lands on a 0, but hopping 1 reaches the 5 and the end.
  • ●A zero is only a problem when nothing reaches past it.
  • ●You don't need the route, only the farthest index reachable so far.
  • ●If i > farthest, index i is unreachable and so is the last index.
  • ●One pass, O(n) time, O(1) space.
Step-by-step visual sheet: Jump Game
The full visual sheet, made with Edithly.Open the interactive sheet

The riddle: Mia and the river stones

Mia is crossing a river on five stones marked 2, 5, 0, 0, 1. The number on each stone is the most stones she can hop forward from there. She can always hop fewer. She starts on the first stone. Can she reach the last one?

Most people's first move is to hop as far as possible. From the 2, she hops 2 and lands on a 0. She can't move, so they answer no. That's wrong. If she hops just 1, she lands on the 5, and from there the last stone is only 3 away. She's across.

That's LeetCode 55, Jump Game, in a single picture: you get an array of non-negative numbers, you start at index 0, each value is your maximum jump from that index, and you return True if the last index is reachable.

Small tests first

A few tiny inputs show what matters and what doesn't:

Input Answer Why
[0] True You're already on the last index
[1, 0] True Hop 1 and you're there
[0, 1] False Stuck at the start
[2, 0, 1] True The 2 jumps right over the 0
[3, 2, 1, 0, 4] False Every route lands on the 0 at index 3

So a zero isn't automatically fatal. What kills [3, 2, 1, 0, 4] is that no index reaches past index 3.

The slow way: try every route

You could try every hop length from every index. From the 3 you'd try hops of 1, 2 and 3, then branch again from each landing spot. That tree grows exponentially. Even if you remember which indices you've already solved, you still check every jump from every index, which is about n squared work. With 10,000 numbers that's on the order of 100 million checks.

The idea: one number, farthest

You never actually care which route got you somewhere. You only care how far you could get. So keep one number, farthest, the greatest index reachable so far. It starts at 0.

At each index i:

  1. If i > farthest, no earlier index could land on i or jump past it. Return False.
  2. Otherwise i is reachable, and from it you can get to i + nums[i]. Set farthest = max(farthest, i + nums[i]).

If you get through the whole array without hitting a gap, return True. Steps 2 to 4 on the visual sheet above build exactly this: the boundary, the update, and the gap check.

Worked trace

On [2, 5, 0, 0, 1]:

i nums[i] i > farthest? farthest after
0 2 no 2
1 5 no 6

Six is already past the last index, 4, so the answer is True. The two zeros never mattered: farthest had flown past them.

On [3, 2, 1, 0, 4]:

i nums[i] i > farthest? farthest after
0 3 no 3
1 2 no 3
2 1 no 3
3 0 no 3
4 4 yes, 4 > 3 stop

Index 4 is past the boundary, so the answer is False.

Python code

def can_jump(nums):
    farthest = 0
    for i, jump in enumerate(nums):
        if i > farthest:
            return False
        farthest = max(farthest, i + jump)
    return True

can_jump([2, 5, 0, 0, 1]) returns True and can_jump([3, 2, 1, 0, 4]) returns False. You can also return True early as soon as farthest >= len(nums) - 1, but the plain loop is already linear.

Complexity

The scan visits each index once, so it's O(n) time. It keeps a single integer, so it's O(1) extra space. Compare that with about n squared checks for the table version: for 10,000 numbers, 10,000 checks instead of roughly 100 million.

The trap

The tempting greedy, always take the biggest jump, commits to one route too early. The correct greedy doesn't pick a route at all. It keeps the whole reachable range, from index 0 up to farthest, and only grows it. The same running-best idea shows up in Maximum Subarray and Best Time to Buy and Sell Stock.

Frequently asked questions

Why doesn't taking the biggest jump work in Jump Game?

The biggest jump can land on a dead end. On [2, 5, 0, 0, 1], jumping 2 from the start lands on a 0 and you're stuck. Jumping just 1 lands on the 5, which reaches the last index. Tracking the farthest reach covers every jump length at once.

What is the time complexity of Jump Game?

The greedy scan looks at each index once, so it's O(n) time and O(1) extra space. Trying every route grows exponentially, and a dynamic programming table that checks every jump from every index is about O(n^2).

Does a 0 in the array mean the answer is False?

No. [2, 0, 1] is True because the 2 jumps right over the 0. A 0 only blocks you when farthest stops at that index, like the 0 at index 3 in [3, 2, 1, 0, 4].

Is Jump Game greedy or dynamic programming?

Both work. Dynamic programming marks which indices are reachable, which costs O(n^2) in the worst case. The greedy version keeps only the farthest reachable index, which is the summary of that whole table, so it runs in O(n).

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free