Skip to content
thesarfo

Reference

Jump Game

Determining if the last index is reachable by tracking the farthest reachable index in a single pass.

views 0

Each element is the max forward jump length from that position; can you reach the last index from the first? [2,3,1,1,4]true. (LeetCode 55)

Key idea: track the farthest reachable index (maxReach) as you scan, instead of trying every possible jump. If the current index ever exceeds maxReach, it’s unreachable — fail immediately.

class Solution {
public boolean canJump(int[] nums) {
int maxReach = 0;
for (int i = 0; i < nums.length; i++) {
if (i > maxReach) {
return false;
}
maxReach = Math.max(maxReach, i + nums[i]);
if (maxReach >= nums.length - 1) {
return true;
}
}
return false;
}
}

Time: O(n). Space: O(1).