LeetCode #45 Medium

Jump Game II

Jump Game II is LeetCode 45 (Medium). Your starting point is the first index of the array nums. From index i you may move forward by any distance from 1 up to nums[i]. Return the minimum number of jumps that lands you on the last index.

  • nums has 1 to 10⁴ values, each from 0 to 1000.
  • The tests promise the last index is reachable, so the code never has to detect being stuck.

A table of best counts per index works, but each index may update up to 1000 others. Grouping indices by jump count needs a single scan and two variables.

Constraints
  • 1 <= nums.length <= 10⁴
  • 0 <= nums[i] <= 1000
  • It's guaranteed that you can reach nums[n - 1].
greedyarraybfs
Open on LeetCode ↗
02

Intuition

Sort the indices by how many jumps it takes to first reach them. That is a breadth-first search, and on this array every BFS level is a block of consecutive indices.

  • Levels are blocks: if one jump can land on index x, it can also land on every index between the take-off point and x. So level 0 is index 0, and each next level starts right after the previous one ends.
  • Two numbers replace the queue: a level is known by its last index, frontier_end. While scanning it, keep next_end, the furthest i + nums[i] seen; that is where the next level ends.

When the scan reaches the end of a level, one more jump is spent. The answer is the level that holds the last index.

How to spot this pattern

Fewest moves from a start to a goal is a BFS question. Here every move goes forward and can stop anywhere in a range, so each BFS level is an interval and its end index is all you need to store.

03

Approach

Try it first

Before reading on: group the indices by the fewest jumps that reach them and look at what shape each group has. Aim for O(n) time and O(1) extra space.

1

Start with level 0

Set jumps, frontier_end and next_end to 0. With no jumps spent you can only be on index 0, so level 0 is that single index and it ends at 0.

2

Scan each index and record its reach

For i from 0 to n − 2, set next_end = max(next_end, i + nums[i]). Every index in the current level is a possible take-off point for the next jump, so the next level ends at the best reach among them.

3

Spend a jump at the end of a level

When i == frontier_end, every index reachable with jumps jumps has been scanned. Add one to jumps and set frontier_end = next_end; the indices just after the old end up to the new end form the new level.

4

Why the count is the minimum

After k jumps are counted, frontier_end is the furthest index any k-jump path reaches. A k-jump path takes off from an index reached in k − 1 jumps, and all of those were scanned, so nothing can reach further. The first level holding the last index is therefore optimal.

5

Return the jump count

When the scan finishes, jumps is the number of the level that contains the last index. Because the input guarantees the end is reachable, no extra check is needed before returning it.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def jump(self, nums: List[int]) -> int:
▶3 jumps = 0
▶4 frontier_end = next_end = 0
▶5 for i in range(len(nums) - 1):
▶6 next_end = max(next_end, i + nums[i])
▶7 if i == frontier_end:
▶8 jumps += 1
▶9 frontier_end = next_end
▶10 return jumps
nums2031121344startindex 0jump 1jump 2start: level 0 is just index 0
jumps0
frontier_end0last index of this level
next_end0furthest reach found
Start. Zero jumps reach only index 0, so that index is level 0 on its own. Every index scanned inside a level is a place one more jump can start from, so scanning a level tells us where the next level ends.
nums20i31121344startindex 0jump 1indices 1–2jump 2index 0 reaches 2 → next level grows to 2
jumps0
frontier_end0last index of this level
next_end2furthest reach found
i + nums[i]2
From index 0 one jump lands anywhere up to index 2. That beats the old best, so the next level now ends at 2. Index 0 is also the last index of level 0.
nums20i31121344startindex 0jump 1indices 1–2jump 2level 0 ends → spend jump 1
jumps1
frontier_end2last index of this level
next_end2furthest reach found
Level 0 is used up. Every index that 0 jumps can reach has been scanned, so going further needs jump 1. The best any of them could do was index 2, which makes indices 1 to 2 level 1.
nums2031i121344startindex 0jump 1indices 1–2jump 2indices 3–4index 1 reaches 4 → next level grows to 4
jumps1
frontier_end2last index of this level
next_end4furthest reach found
i + nums[i]4
From index 1 one jump lands anywhere up to index 4. That beats the old best, so the next level now ends at 4. Index 1 is not the end of level 1 (that is index 2), so keep scanning it before spending a jump.
nums203112i1344startindex 0jump 1indices 1–2jump 2indices 3–4index 2 reaches only 3 → next level stays
jumps1
frontier_end2last index of this level
next_end4furthest reach found
i + nums[i]3
From index 2 one jump lands anywhere up to index 3. The next level already reaches 4, so nothing changes. Index 2 is also the last index of level 1.
nums203112i1344startindex 0jump 1indices 1–2jump 2indices 3–4level 1 ends → spend jump 2, it holds the goal
jumps2
frontier_end4last index of this level
next_end4furthest reach found
Level 1 is used up. Every index that 1 jump can reach has been scanned, so going further needs jump 2. The best any of them could do was index 4, which makes indices 3 to 4 level 2. The last index is inside it, and a later scan can never lower the count.
nums20311213i44startindex 0jump 1indices 1–2jump 2indices 3–4index 3: not the end of level 2 → no jump
jumps2
frontier_end4last index of this level
next_end4furthest reach found
i + nums[i]4
From index 3 one jump lands anywhere up to index 4. Level 2 already holds the last index, so no reach found now can change the count. Index 3 is not the end of level 2 (that is index 4), so keep scanning it before spending a jump.
nums2031121344istartindex 0jump 1indices 1–2jump 2indices 3–4index 4 is the goal → stop before scanning it
jumps2
frontier_end4last index of this level
next_end4furthest reach found
The loop ends at n − 2. Index 4 is the last index and it is also the end of level 2. Scanning it would match i == frontier_end and count jump 3, a jump that starts at the goal and is never taken.
nums2031121344startindex 0jump 1indices 1–2jump 2indices 3–4last index 4 is in level 2 → answer 2
jumps2
frontier_end4last index of this level
next_end4furthest reach found
return2
Answer 2. Levels are filled in order of jump count, so the first level that holds the last index gives the fewest jumps. Each index was scanned once: O(n) time and two numbers of state.
05

Common pitfalls

Scanning the last index

✗ Wrong
for i in range(len(nums)):
✓ Right
for i in range(len(nums) - 1):

On [1, 1, 1] index 2 is both the goal and the end of level 2. Scanning it matches i == frontier_end and counts a third jump that would start from the goal, so the code returns 3 instead of 2.

Always taking the longest single jump

✗ Wrong
i += nums[i]
jumps += 1
✓ Right
next_end = max(next_end, i + nums[i])

On [2, 3, 1, 1, 4] the longest jump from index 0 lands on index 2, whose value is only 1, and the walk needs 3 jumps. Landing on index 1 (value 3) reaches the end in 2. The right choice is the take-off point with the furthest reach, not the furthest landing spot.

Counting a jump each time the reach improves

✗ Wrong
if i + nums[i] > next_end:
    jumps += 1
✓ Right
if i == frontier_end:
    jumps += 1
    frontier_end = next_end

On [1, 2, 3, 4, 5] the reach improves at indices 0, 1, 2 and 3, giving 4, but the path 0 → 1 → 3 → 4 needs only 3. Several improvements can happen inside one level, and they all belong to the same next jump.

06

Edge cases

One element, [0]

The loop covers indices 0 to n − 2, which is empty, so 0 is returned. You start on the last index.

A zero inside a level, [2, 0, 1]

Index 1 cannot move, but it sits inside level 1 next to index 2, which index 0 already reaches. Its reach of 1 changes nothing and the answer is 1.

Reach past the end, [5, 1, 1]

next_end becomes 5, beyond the last index 2. It is only compared with i, so no clamping is needed and the answer is 1.

07

Complexity

Time
O(n)
Space
O(1)
Each index is scanned once, and frontier_end plus next_end are the whole BFS state.
08

Jump Game II FAQ

Does a DP table also work?

Yes. Let dp[i] be the fewest jumps to reach index i, and from each index relax up to nums[i] later indices. That is O(n · max(nums)), about 10⁷ steps at the limits, plus O(n) memory. It passes, but the level scan does the same job in one pass because it never revisits an index.

How do I recover the actual jumps, not just the count?

Next to next_end, remember the index that produced it. At every level end, that index is the best take-off point of the level just finished. The path is those take-off points in order, then the last index; on [2, 3, 1, 1, 4] it is 0 → 1 → 4.

What if the last index might be unreachable?

At a level end, if next_end is not past frontier_end, no jump gets any further and you can return −1. Deciding reachability alone is Jump Game, which only tracks the furthest reach.