Skip to main content
62

Jump Game

Mediumgreedy

Your code isn't saved yet: refreshing or leaving the page resets it. Saved progress comes with user accounts.

This problem uses:GreedyRead the beginner guide

You start at index 0 of an array nums. Each element nums[i] is the maximum jump length from position i: you may jump forward by any distance from 0 to nums[i].

Return true if you can reach the last index, otherwise false.

Understand the problem

  • From index i, you can land on any index from i + 1 up to i + nums[i]. You don't have to jump the full length.
  • A 0 means you can't move forward from that spot.
  • You only need to say whether the last index is reachable, not how.

Example 1

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

Jump 1 step to index 1, then 3 steps to index 4 (the last).

Example 2

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

Every route ends up on index 3, whose jump length is 0. Index 4 is out of reach.

Example 3

text
Input:  nums = [0]
Output: true

You're already standing on the last index.

Constraints

  • 1 <= nums.length <= 10^4
  • 0 <= nums[i] <= 10^5

Hints

Stuck? Open one hint at a time. Each one gives away a little more than the last.

    The idea in one sentence

    Walk left to right tracking the farthest index you can reach; if you ever stand on an index beyond it you're stuck, and if it reaches the end you've made it.

    The naive approach (and why it's slow)

    Try every possible jump from every position, recursively. With big jump lengths, the number of routes grows exponentially. Even a DP version ("canReach[i] is true if some earlier reachable j can jump to i") checks every pair: O(n²).

    The key insight: only the farthest reach matters

    You don't need to know how you reach an index, only how far you can get. Keep one number, farthest: the furthest index reachable so far.

    Why is one number enough? Because a jump can stop anywhere along its length. If you can reach index 7, you can also reach 1, 2, ..., 6. So the reachable indexes are always a solid block from 0 to farthest.

    Walk left to right:

    1. If the current index i is beyond farthest, there's a gap you can't cross: return false.
    2. Otherwise, from i you can reach i + nums[i]: update farthest = max(farthest, i + nums[i]).
    3. If farthest reaches the last index, return true.

    This is a greedy algorithm: at each step it keeps only the best thing so far (the farthest reach) and never reconsiders.

    Step by step

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

    inums[i]i reachable?i + nums[i]farthest
    03yes (0 ≤ 0)33
    12yes (1 ≤ 3)33
    21yes (2 ≤ 3)33
    30yes (3 ≤ 3)33
    44no (4 > 3)

    Answer: false. Every index can only get you as far as 3.

    For nums = [2, 3, 1, 1, 4]: at i = 0, farthest = 2; at i = 1, farthest = max(2, 1 + 3) = 4, which is the last index: true.

    The code

    The reference solution is below, with comments on every important line.

    How fast is it?

    • Time: O(n). One pass.
    • Memory: O(1). One variable.

    Common mistakes

    • Always jumping the maximum length. On [2, 3, 0, 0, 4], jumping 2 from index 0 lands on a 0 and gets stuck, but jumping 1 reaches the 3, which gets you to the end. Tracking farthest considers every option at once.
    • Checking i > farthest after updating farthest. Check first: you must be able to stand on i before you can jump from it.
    • Using BFS/DFS over every jump. It works, but it's O(n²) in the worst case.

    Going deeper

    • Jump Game II asks for the fewest jumps to reach the end. It's the same idea, processed in "levels": the indexes reachable with 1 jump, then 2 jumps, and so on, like BFS.
    • The DP view, canReach[i], is the slow, safe version. Greedy algorithms are often "a DP where you can prove only one number matters".

    Reference solution

    js
    /**
     * @param {number[]} nums
     * @return {boolean}
     */
    export default function canJump(nums) {
      // Every index from 0 to farthest is reachable (a jump can stop anywhere along its length).
      let farthest = 0
      for (let i = 0; i < nums.length; i++) {
        if (i > farthest) return false // a gap nothing before could cross
        // From i we can reach up to i + nums[i].
        farthest = Math.max(farthest, i + nums[i])
        if (farthest >= nums.length - 1) return true // the last index is in reach
      }
      return true
    }
    ts
    export default function canJump(nums: number[]): boolean {
      // Every index from 0 to farthest is reachable (a jump can stop anywhere along its length).
      let farthest = 0
      for (let i = 0; i < nums.length; i++) {
        if (i > farthest) return false // a gap nothing before could cross
        // From i we can reach up to i + nums[i].
        farthest = Math.max(farthest, i + nums[i])
        if (farthest >= nums.length - 1) return true // the last index is in reach
      }
      return true
    }
    python
    def can_jump(nums: list[int]) -> bool:
        # Every index from 0 to farthest is reachable (a jump can stop anywhere along its length).
        farthest = 0
        for i, jump in enumerate(nums):
            if i > farthest:
                return False  # a gap nothing before could cross
            # From i we can reach up to i + jump.
            farthest = max(farthest, i + jump)
            if farthest >= len(nums) - 1:
                return True  # the last index is in reach
        return True