1. Farthest reach
Track the farthest index reachable so far.
let reach = 0Typorithm · medium
Step 1 of 8 · intro card
Greedy
Walk left to right, tracking the farthest reachable index; fail the moment an index is unreachable.
Each value tells you the maximum jump length from that index.
The greedy insight is that only the farthest reach matters, not which exact jumps you take.
If you ever stand on an index beyond the farthest reach, the last index is impossible.
Track one number: the farthest reach.
Fail if i passes reach.
reach = max(reach, i + nums[i]).
function canJump(nums) {
let reach = 0;
for (let i = 0; i < nums.length; i += 1) {
if (i > reach) {
return false;
}
reach = Math.max(reach, i + nums[i]);
}
return true;
}Connect each part of the code to the concept before you type.
Track the farthest index reachable so far.
let reach = 0Walk every index from left to right.
for (let i = 0; i < nums.length; i += 1)If the current index is past the farthest reach, it can never be reached.
if (i > reach)From index i you can reach i + nums[i]; keep the farthest.
reach = Math.max(reach, i + nums[i])Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
InputJump lengths 2,3,1,1,4
Follow temporary values, local state and supporting structures at this step.
No temporary values have been saved in this frame yet.
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state