If i > reach, return false; otherwise extend reach. Reaching n − 1 means true.
Approach 1
class Solution {
public boolean canJump(int[] nums) {
int reach = 0;
for (int i = 0; i < nums.length; i++) {
if (i > reach) return false;
reach = Math.max(reach, i + nums[i]);
}
return true;
}
}
Verdict: A DP would be O(n²) for the same answer.
Before you submit
Edge cases and common mistakes
Test these inputs
Single element (true)
A zero you can jump over
Mistakes people make
Always taking the biggest jump from the current index.
Interview
Follow-up questions
What if you can jump in both directions (Jump Game III)?