Jump Game
Your code isn't saved yet: refreshing or leaving the page resets it. Saved progress comes with user accounts.
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 fromi + 1up toi + nums[i]. You don't have to jump the full length. - A
0means you can't move forward from that spot. - You only need to say whether the last index is reachable, not how.
Example 1
Input: nums = [2, 3, 1, 1, 4]
Output: trueJump 1 step to index 1, then 3 steps to index 4 (the last).
Example 2
Input: nums = [3, 2, 1, 0, 4]
Output: falseEvery route ends up on index 3, whose jump length is 0. Index 4 is out of reach.
Example 3
Input: nums = [0]
Output: trueYou're already standing on the last index.
Constraints
1 <= nums.length <= 10^40 <= 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:
- If the current index
iis beyondfarthest, there's a gap you can't cross: returnfalse. - Otherwise, from
iyou can reachi + nums[i]: updatefarthest = max(farthest, i + nums[i]). - If
farthestreaches the last index, returntrue.
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):
i | nums[i] | i reachable? | i + nums[i] | farthest |
|---|---|---|---|---|
| 0 | 3 | yes (0 ≤ 0) | 3 | 3 |
| 1 | 2 | yes (1 ≤ 3) | 3 | 3 |
| 2 | 1 | yes (2 ≤ 3) | 3 | 3 |
| 3 | 0 | yes (3 ≤ 3) | 3 | 3 |
| 4 | 4 | no (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 a0and gets stuck, but jumping 1 reaches the3, which gets you to the end. Trackingfarthestconsiders every option at once. - Checking
i > farthestafter updatingfarthest. Check first: you must be able to stand onibefore 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
/**
* @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
}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
}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