Jump Game
You start at index 0 of an integer array nums. The value nums[i] is the longest jump you may take from index i: from there you can move forward to any index from i + 1 up to i + nums[i] (a value of 0 means you are stuck on that square).
Return true if some sequence of jumps lands you on the last index, and false otherwise.
Example 1
Example 2
Example 3
Constraints
1 <= nums.length <= 1040 <= nums[i] <= 105
Pattern Path · Intervals & Greedy · problem 114 of 169
j, can you also reach every index before j?0 .. reach. Scan left to right and keep extending reach with i + nums[i].reach, you are stuck.Start simple
Mark reachable indices with DP: for each reachable i, mark i+1 .. i+nums[i]. That is O(n · max jump) time, O(n2) in the worst case.
The key idea
The key observation is that the set of reachable indices has no gaps. If you can land on index j, the jump that got you there passed over every index between its start and j, and it could have stopped on any of them, because jumps may be shorter than the maximum. So everything reachable is a prefix 0 .. reach.
That turns the problem into tracking one number. Walk through the array; at index i:
- If
i > reach, indexicannot be reached, and nothing after it can either: returnfalse. - Otherwise extend
reach = max(reach, i + nums[i]), and if it already covers the last index, returntrue.
This is a greedy argument: we never commit to a jump, we just keep the furthest frontier, and since the frontier can only grow, one left-to-right pass decides the answer.
Complexity
- Time:
O(n) - Space:
O(1)
Python
class Solution:
def canJump(self, nums):
reach = 0 # furthest index known to be reachable
last = len(nums) - 1
for i, jump in enumerate(nums):
if i > reach:
return False # stuck: nothing reaches index i
reach = max(reach, i + jump)
if reach >= last:
return True
return True
JavaScript
var canJump = function(nums) {
let reach = 0; // furthest index known to be reachable
const last = nums.length - 1;
for (let i = 0; i < nums.length; i++) {
if (i > reach) return false; // stuck: nothing reaches index i
reach = Math.max(reach, i + nums[i]);
if (reach >= last) return true;
}
return true;
};
What the interviewer is checking
The interviewer wants to see you move from a search or DP formulation to the one-variable greedy, and justify it with the "reachable set is a prefix" argument.
Mistakes to avoid
- Treating
nums[i]as the exact jump length instead of the maximum. - Not stopping when
ipassesreach, which lets the scan "jump over" a dead zero. - Forgetting that a single-element array is already at the end (true even if the value is 0).
- Using DFS or BFS over all jumps, which can be quadratic with large values.
Follow-ups to expect
- Return the minimum number of jumps. (Greedy BFS over reach windows.)
- What if jumps may go left or right? (BFS on the index graph.)
- Solve it scanning from the right. (Track the leftmost index that reaches the end.)
In real software
Reachability by an expanding frontier shows up in network hop planning and in checking whether overlapping coverage ranges (sensors, radio towers, cached byte ranges) cover a whole span without a gap.