1. Level boundary
end marks the farthest index reachable with the current number of jumps.
let end = 0Typorithm · hard
Step 1 of 8 · intro card
Greedy
Treat each jump as a BFS level: count a jump each time you exhaust the current reachable range.
Jump Game II asks for the fewest jumps to reach the last index, not just whether it is reachable.
Each 'level' is the range of indexes reachable with the current jump count, like a BFS frontier.
When the scan hits the end of the current level, you must spend one jump and extend to the farthest seen.
Levels behave like BFS frontiers.
Bump jumps when i === end.
Loop stops before the last index.
function minJumps(nums) {
let jumps = 0;
let end = 0;
let farthest = 0;
for (let i = 0; i < nums.length - 1; i += 1) {
farthest = Math.max(farthest, i + nums[i]);
if (i === end) {
jumps += 1;
end = farthest;
}
}
return jumps;
}Connect each part of the code to the concept before you type.
end marks the farthest index reachable with the current number of jumps.
let end = 0Track the farthest index reachable from anywhere in the current level.
farthest = Math.max(farthest, i + nums[i])When i reaches the level boundary, one more jump is forced.
if (i === end)The new boundary is the farthest we discovered this level.
end = farthestRead 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