Skip to main content

55. Jump Game

medium

Each index nums[i] is your max jump length from there. Can you reach the last index? A 1D DP or one-pass greedy tracking the farthest reachable index.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position. Return true if you can reach the last index, or false otherwise.

Constraints

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

Examples

Example 1

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

Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2

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

Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Hints

Progressive — try the first before opening the next.

Hint 1

DP: dp[i] = true if index i is reachable. dp[i] = exists j with j + nums[j] >= i and dp[j] is true. O(n^2).

Hint 2

Greedy: maintain farthest = furthest index reachable so far. As you scan, if i > farthest you cannot proceed.

Hint 3

Otherwise update farthest = max(farthest, i + nums[i]). Return whether farthest >= last index.

Solution approach

Reveal approach

Greedy 1D sweep. Maintain farthest = the maximum index reachable using any valid prefix of jumps. Initialize farthest = 0. For i from 0 to n-1: if i > farthest then return false (no jump landed here, so we're stuck); else farthest = max(farthest, i + nums[i]). If farthest >= n-1 return true immediately as an early exit, otherwise continue. After the loop return true. O(n) time, O(1) space. The equivalent O(n^2) DP defines dp[i] as whether i is reachable, with dp[0] = true and dp[i] = any j < i with dp[j] = true and j + nums[j] >= i.

Complexity

Time
O(n)
Space
O(1)

Related patterns

  • dynamic-programming
  • greedy

Related problems

Asked at

Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).

  • Amazon
  • Microsoft
  • Google
  • Apple

Practice these live with InterviewChamp.AI

Drill Jump Game and DP 1D problems under real interview conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →