Typorithm · hard

Jump Game II

▷ Interactive algorithm labjump-game-iigreedymin-jumpslevel-rangegreedy-choice
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Jump Game II

0%

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.

Preparing algorithm lab…
01

Levels behave like BFS frontiers.

02

Bump jumps when i === end.

03

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;
}

Read the implementation

Connect each part of the code to the concept before you type.

1. Level boundary

end marks the farthest index reachable with the current number of jumps.

let end = 0

2. Extend frontier

Track the farthest index reachable from anywhere in the current level.

farthest = Math.max(farthest, i + nums[i])

3. Level exhausted

When i reaches the level boundary, one more jump is forced.

if (i === end)

4. Next level

The new boundary is the farthest we discovered this level.

end = farthest

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Jump Game II: Visual Walkthrough & Practice · Typorithm | ApplyDjinn