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.
numshas 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.
- 1 <= nums.length <= 10⁴
- 0 <= nums[i] <= 1000
- It's guaranteed that you can reach nums[n - 1].
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, keepnext_end, the furthesti + 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.
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.
Approach
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.
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.
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.
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.
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.
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.
Code in Python, C++ and Java
i == frontier_end and count jump 3, a jump that starts at the goal and is never taken.i == frontier_end and count jump 3, a jump that starts at the goal and is never taken.Common pitfalls
Scanning the last index
for i in range(len(nums)):
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
i += nums[i] jumps += 1
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
if i + nums[i] > next_end:
jumps += 1if i == frontier_end:
jumps += 1
frontier_end = next_endOn [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.
Edge cases
[0]The loop covers indices 0 to n − 2, which is empty, so 0 is returned. You start on the last index.
[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.
[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.
Complexity
frontier_end plus next_end are the whole BFS state.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.