Practice Jump Game
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
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.
Source: Jump Game on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
nums = [2,3,1,1,4]
true
# Example 2
nums = [3,2,1,0,4]
false
Constraints
How to solve Jump Game
Original notes for interview preparation, written for this problem. Reviewed .
Key insight
Track the farthest index reachable so far while scanning left to right. The last index is reachable exactly when you never arrive at an index beyond that frontier before the frontier covers the end.
Why brute force is not enough
Treat each index as a graph node with an edge to every index within its jump range and run DFS or BFS. With nums.length up to 10^4 and nums[i] up to 10^5, the edge count approaches 10^8, and memoized recursion is still O(n^2) in the worst case.
Approach
- Maintain one number, the farthest index reachable using positions examined so far, starting at 0. Invariant: every index up to this value is reachable from index 0.
- Scan indices in order. If the current index is beyond the farthest reachable, return false; nothing to the right can ever be reached.
- Otherwise extend the farthest value to the maximum of itself and the current index plus its jump length.
- Return true as soon as the farthest value reaches the last index, or when the scan finishes without stranding.
- The backward greedy is equivalent: walk from the right keeping the leftmost index known to reach the end; an index qualifies if its jump reaches that marker, and the answer is whether index 0 qualifies.
Walkthrough of Example 2
nums = [3,2,1,0,4] → false
- Farthest starts at 0. At index 0 the value is 3, so farthest becomes max(0, 0 + 3) = 3.
- At index 1 the value is 2; 1 + 2 = 3, so farthest stays 3.
- At index 2, 2 + 1 = 3; at index 3, 3 + 0 = 3. Farthest stays 3.
- Index 4 exceeds farthest (3), so the scan is stranded.
- Return false: every reachable position lands at or before index 3, whose jump length of 0 is a dead end.
Time and space
Time O(n) · Space O(1). n is nums.length; each index is visited once and a single frontier integer is kept.
Interview pitfalls
- Returning false when nums[0] is 0: a one-element array is already at the last index and must return true.
- Over-special-casing zeros; a zero is only fatal when it is the frontier, so 2, 0, 0 is true.
- Testing the stranded condition after updating the frontier with the current index, which hides the gap.
- Using index plus jump length as an array index; it can exceed the array since jumps reach 10^5, so compare it, never dereference it.
- Stopping at the O(n^2) dynamic program; it passes 10^4 but the interviewer expects the linear greedy.
Follow-up questions interviewers ask
- Return the minimum number of jumps needed to reach the end (Jump Game II).
- Allow jumping left or right by exactly nums[i] and ask whether any index holding 0 is reachable (Jump Game III).
- Attach a cost to landing on each index and minimize the total cost of reaching the end.
Further reading on the technique: Greedy algorithm
Related LeetCode Problems
Practice more problems similar to Jump Game.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.