55. Jump Game
mediumEach 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^40 <= nums[i] <= 10^5
Examples
Example 1
nums = [2,3,1,1,4]trueExplanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2
nums = [3,2,1,0,4]falseExplanation: 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.
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
- 45. Jump Game II
- 1306. Jump Game III
- 871. Minimum Number of Refueling Stops
Asked at
Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).
- Amazon
- Microsoft
- 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 →