Given integer array nums where nums[i] is the max jump length from index i, return true if you can reach the last index from index 0.
Example
Input: nums = [2,3,1,1,4]
Output:true// Jump 1 step from 0โ1, then 3 steps from 1โ4.Input: nums = [3,2,1,0,4]
Output:false// Stuck at index 3 (value 0).
From index 0, try every possible jump length (1 to nums[0]). Recurse from each landing index. Return true if any path reaches the end.
Why it works
Exhaustively explores all jump sequences.
Why we can do better
Problem: Exponential branching. We don't need to track paths โ just whether we can reach each position. A single left-to-right pass tracking the farthest reachable index suffices.
Java โ DP (bottom-up)
classSolution {
publicbooleancanJump(int[] nums) {
int n = nums.length;
boolean[] dp = newboolean[n];
dp[0] = true;
for (int i = 1; i < n; i++)
for (int j = 0; j < i; j++)
if (dp[j] && j + nums[j] >= i) { dp[i] = true; break; }
return dp[n - 1];
}
}
Python โ DP (bottom-up)
classSolution:
defcanJump(self, nums: list[int]) -> bool:
n = len(nums)
dp = [False] * n
dp[0] = Truefor i in range(1, n):
for j in range(i):
if dp[j] and j + nums[j] >= i:
dp[i] = True; breakreturn dp[-1]
Metric
Value
Time
O(nยฒ)
Space
O(n)
03
Section Three ยท Approach 2
Greedy โ O(n) time, O(1) space
Track the farthest reachable index. Scan left to right: if the current index exceeds the farthest reachable, you're stuck. Otherwise, update the farthest as max(farthest, i + nums[i]).
๐ก Mental model: Imagine walking along a hallway with fuel tanks on the floor. Each fuel tank tells you how many more steps you can travel. As you walk, you keep track of the farthest point your fuel can carry you. If you ever step past your fuel range, you're stranded. If your fuel range reaches or passes the exit, you escape.
Algorithm
Initialize farthest = 0.
For i from 0 to n-1: if i > farthest, return false. Else farthest = max(farthest, i + nums[i]).
Return true (loop completed without getting stuck).
๐ฏ When to recognize this pattern: "Can you reach the end?" with forward-only movement and variable step sizes. The signal: reachability on a 1D array. Track farthest point greedily. Also the foundation for LC 45 (Jump Game II โ minimum jumps).
Why greedy is correct: If you can reach index i, you can reach every index between 0 and i + nums[i]. The farthest reachable monotonically increases (or stays). No backtracking needed โ reaching farther is always at least as good.
04
Section Four ยท Trace
Visual Walkthrough
Trace for nums = [2, 3, 1, 1, 4].
Jump Game โ Greedy Reachability
05
Section Five ยท Implementation
Code โ Java & Python
Java โ Greedy
classSolution {
publicbooleancanJump(int[] nums) {
int farthest = 0;
for (int i = 0; i < nums.length; i++) {
if (i > farthest) returnfalse; // can't reach i
farthest = Math.max(farthest, i + nums[i]);
}
returntrue;
}
}
Python โ Greedy
classSolution:
defcanJump(self, nums: list[int]) -> bool:
farthest = 0for i, n in enumerate(nums):
if i > farthest: returnFalse
farthest = max(farthest, i + n)
returnTrue
06
Section Six ยท Analysis
Complexity Analysis
Approach
Time
Space
Trade-off
DP (bottom-up)
O(nยฒ)
O(n)
Boolean array; inner loop per index
DP backward (last good)
O(nยฒ)
O(n)
Track last good index from right
Greedy โ optimal
O(n)
O(1)
Single variable; one pass
07
Section Seven ยท Edge Cases
Edge Cases & Pitfalls
Case
Input
Why It Matters
Single element
[0]
Already at end. Return true.
All zeros
[0,0,0]
Stuck at index 0. Return false (n>1).
Large first jump
[10,0,0,0]
Jump from 0 covers entire array. True.
Zero at end
[2,3,1,0]
Zero at last index doesn't matter โ you're already there.
Stuck at zero
[1,0,1]
Reach index 1 (value 0), can't proceed. False.
โ Common Mistake: Checking nums[i] == 0 and immediately returning false. A zero at index i only matters if farthest == i (you can't get past it). If farthest > i, you bypassed the zero via an earlier longer jump.