Browse curriculum

Jump Game (LeetCode 55)

Jump Game (LeetCode 55): determine if you can reach the last index.

Examples

Input:nums = [2,3,1,1,4]
Output:true
Explain:

From index 0 move 1 place to index 1; its value of 3 then reaches the last index.

Input:nums = [3,2,1,0,4]
Output:false
Explain:

Every path lands on index 3, whose jump length is 0, so index 4 is never reached.

Input:nums = [0]
Output:true
Explain:

You start on the last index, so there is nothing to jump.

Problem Understanding

You are given an array nums. nums[i] is the maximum jump length from that position. Return true if you can reach the last index.

Strategy

Track the furthest index reachable so far.

  1. Start with reachable = 0: the index you begin on.
  2. Walk i from 0 upward.
  3. If i > reachable, nothing can land on i: return false.
  4. Otherwise update reachable = max(reachable, i + nums[i]).
  5. As soon as reachable reaches the last index, return true.

Interactive Visualization

✓ = REACHABLE · THE MARKS ARE ALWAYS A PREFIX · OUTLINED = HOW FAR THIS JUMP CAN LAND

START ON INDEX 0

0

1

2

3

4

jump

2
3
1
1
4

reach

✓

furthest reachable

0

last index

4

Answer

:

the rule

past the frontier → false
frontier = the further of itself and i + jump
frontier covers the last index → true

 

You start on index 0, so the furthest reachable index is 0. The goal is index 4

Next

Visit index 0, which can jump up to 2

Free preview: the first 3 of 6 steps

nums

The top row is the jump length at each index; the bottom row marks every index known to be reachable. The marks are always a prefix, which is exactly why one number, the frontier, can stand for all of them. The index being visited is filled, and the furthest cell its jump can land on is outlined.

Try Example 2: every path lands on the 0 at index 3, and the walk stops at index 4, past the frontier.

Dry Run

nums = [2, 3, 1, 1, 4], last index 4:

i nums[i] i > reachable? i + nums[i] reachable after
0 2 No (0 ≤ 0) 2 2
1 3 No (1 ≤ 2) 4 4

After index 1 the frontier is 4, which covers the last index, return true without visiting the rest.

Edge Cases & Common Mistakes

Consider these scenarios:

  • A zero at the start is only fatal if there is more to cross. [0] answers true (you are already at the end) but [0, 1] answers false.

  • A zero in the middle is only a trap if every path lands on it. In [2, 0, 0] the first jump clears both zeros; in [3, 2, 1, 0, 4] every path is funnelled onto index 3.

  • Stop as soon as the frontier covers the end. There is no need to visit the rest of the array once the last index is reachable.

Solution

One pass carrying the frontier, stopping early either way: the lines light up as the figure above steps.

Jump Game Code
1var canJump = function(nums) {
2 const last = nums.length - 1;
3 let reachable = 0;
4 for (let i = 0; i < nums.length; i++) {
5 if (i > reachable) {
6 return false;
7 }
8 reachable = Math.max(reachable, i + nums[i]);
9 if (reachable >= last) {
10 return true;
11 }
12 }
13 return true;
14};

Jump Game in Python, Java, C++, TypeScript and C

Jump Game in Python
class Solution:
    def canJump(self, nums: List[int]) -> bool:
        last = len(nums) - 1
        reachable = 0
        for i, jump in enumerate(nums):
            if i > reachable:
                return False
            reachable = max(reachable, i + jump)
            if reachable >= last:
                return True
        return True
Jump Game in Java
class Solution {
    public boolean canJump(int[] nums) {
        int last = nums.length - 1;
        int reachable = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i > reachable) {
                return false;
            }
            reachable = Math.max(reachable, i + nums[i]);
            if (reachable >= last) {
                return true;
            }
        }
        return true;
    }
}
Jump Game in C++
class Solution {
public:
    bool canJump(vector<int>& nums) {
        int last = nums.size() - 1;
        int reachable = 0;
        for (int i = 0; i < (int)nums.size(); i++) {
            if (i > reachable) {
                return false;
            }
            reachable = max(reachable, i + nums[i]);
            if (reachable >= last) {
                return true;
            }
        }
        return true;
    }
};
Jump Game in TypeScript
function canJump(nums: number[]): boolean {
    const last = nums.length - 1;
    let reachable = 0;
    for (let i = 0; i < nums.length; i++) {
        if (i > reachable) {
            return false;
        }
        reachable = Math.max(reachable, i + nums[i]);
        if (reachable >= last) {
            return true;
        }
    }
    return true;
}
Jump Game in C
bool canJump(int* nums, int numsSize) {
    int last = numsSize - 1;
    int reachable = 0;
    for (int i = 0; i < numsSize; i++) {
        if (i > reachable) {
            return false;
        }
        if (i + nums[i] > reachable) reachable = i + nums[i];
        if (reachable >= last) {
            return true;
        }
    }
    return true;
}

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Start at the front

    Set the furthest reachable index to 0: the index you start on.

  2. Walk and extend

    For each index inside the frontier, push the frontier to i + nums[i] if that is further.

  3. Stop early either way

    Return false on reaching an index past the frontier, and true as soon as the frontier covers the last index.

Frequently asked questions

Why is tracking the furthest reachable index enough?

Every index up to the frontier can be reached: the frontier only grows by jumps from indices already inside it, and a jump of length k can stop anywhere in between. So the reachable indices are always a prefix, and its right edge describes all of them.


When does Jump Game return false?

When the walk reaches an index beyond the furthest reachable one. Nothing can land there, and since the frontier can only grow from indices inside it, nothing beyond it can be reached either. It usually happens just past a 0 that every path is forced onto.


Is dynamic programming needed for Jump Game?

No. A DP over "can index i be reached" works but is O(n²) in the worst case. The greedy frontier gets the same answer in O(n) with O(1) space, because reachability is always a prefix. Jump Game II, which asks for the fewest jumps, is where more care is needed.

New to the technique behind this problem? Watch it run step by step:

Greedy Algorithms Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description