mediumGreedyArrayGreedy

Jump Game II

mediumTime: O(n)Space: O(1)

Recognize the pattern

minimum jumps to reach endeach position has a jump rangecount steps not reachability

Brute force idea

If you approach Jump Game II in the most literal way possible, you get this: BFS from index 0, each position enqueues all reachable positions. Processes every reachable index. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.

Better approach

The deeper shift in Jump Game II is this: Greedy BFS: track the farthest reachable index and the end of the current 'level.' When you pass the current level's end, increment jumps and update the level boundary. Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.

Key invariant

The truth you want to protect throughout Jump Game II is this: Each 'jump' covers all positions reachable from the current level. The farthest position reachable from this level becomes the boundary of the next level. You never need to revisit positions. If that remains true after every update, the rest of the reasoning has a stable place to stand.

Watch out for

The trap in Jump Game II usually looks like this: Using actual BFS with a queue — the greedy approach achieves the same result without a queue by tracking the level boundary implicitly. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.

Greedy Pattern