Jump Game II (LeetCode 45): Solution and Explanation

Medium
Array
Dynamic Programming
Greedy

Each value nums[i] is the farthest you can jump forward from index i. Starting at index 0, find the minimum number of jumps needed to reach the last index, which is guaranteed to be reachable.

Variables
No variables to display
DepthFunction Call
Stack empty
0/0

Intuition

A natural first attempt is dynamic programming: let dp[i] be the fewest jumps to reach i, and for each index update every position it can reach. That works, but it costs O(n²) in the worst case because an index with a big jump value updates many cells. There is a lot of redundancy, since we only care about the minimum number of jumps, not about exact paths.

The better framing is breadth-first search. Index 0 is level zero. Every index you can reach in one jump forms level one, every index reachable in two jumps forms level two, and so on. The answer is the level that contains the last index. The key observation is that each level is a contiguous range of indices. If you can reach index 7 in two jumps, you can also reach every index between the end of level one and 7 in two jumps, because jumps can be shorter than the maximum.

So instead of a queue, you keep a window [l, r] for the current level. Scanning that window, you compute farthest, the largest i + nums[i] for any i inside it. The next level is then [r + 1, farthest]. Each time you move to the next window, you have spent one more jump. You stop as soon as the window's right edge reaches the last index.

Because the windows never overlap and each index is scanned exactly once across all levels, the whole thing is linear, and you never store more than a couple of integers.

Approach: Greedy BFS over windows

  1. Set res = 0 jumps and the current window to l = r = 0, which holds only the starting index.
  2. While r < len(nums) - 1, the last index has not been reached yet, so you need another jump.
  3. Scan every index i from l to r and track farthest = max(farthest, i + nums[i]).
  4. Move to the next window by setting l = r + 1 and r = farthest.
  5. Increment res because entering the new window costs one jump.
  6. When the loop exits, r covers the last index, so return res.

Step-by-step example

nums = [2,3,1,1,4]
  1. Start with l = r = 0 and res = 0. The last index is 4, and r = 0 is less than 4, so enter the loop.
  2. Window [0, 0]: index 0 reaches 0 + 2 = 2, so farthest = 2. The next window is [1, 2] and res = 1.
  3. r = 2 is still less than 4. Window [1, 2]: index 1 reaches 1 + 3 = 4 and index 2 reaches 2 + 1 = 3, so farthest = 4.
  4. The next window is [3, 4] and res = 2.
  5. Now r = 4 is not less than 4, so the loop ends and the function returns 2 (jump from 0 to 1, then from 1 to 4).

Code: Python, Java, C++

1class Solution:
2    def jump(self, nums: List[int]) -> int:
3        res = 0
4        l = r = 0
5        
6        while r < len(nums) - 1:
7            farthest = 0
8            for i in range(l, r + 1):
9                farthest = max(farthest, i + nums[i])
10            l = r + 1
11            r = farthest
12            res += 1
13        return res

Time and space complexity

Time

O(n)

The windows partition the indices into disjoint ranges, so the inner loop touches each index at most once over the entire run.

Space

O(1)

Only the window bounds, the jump count and farthest are stored.

Other approaches

Bottom-up dynamic programming (time O(n²), space O(n))

Keep dp[i] as the minimum jumps to reach index i and, for each i, relax every index up to i + nums[i]. It is easy to reason about and generalizes to weighted variants, but it is quadratic when jump values are large.

Greedy with current end (time O(n), space O(1))

Iterate i from 0 to n - 2, updating a global farthest, and whenever i reaches currentEnd, count a jump and set currentEnd = farthest. It is the same BFS idea written as a single loop instead of nested window loops.

Common mistakes

  • Looping while r < len(nums) instead of len(nums) - 1, which counts an extra jump once the last index is already inside the window.
  • Starting the next window at l = r instead of r + 1, which rescans the previous level and can make the loop do extra work.
  • Greedily jumping to the index with the largest value inside the window instead of the one that reaches farthest. What matters is i + nums[i], not nums[i] alone.
  • Forgetting the case of a single element array, where zero jumps are needed and the loop must not run at all.

Frequently asked questions

Why is the greedy approach for Jump Game II correct?

Every index reachable with k jumps forms a contiguous window, and the next window is everything up to the farthest reach from the current one. Counting windows until the last index is covered is exactly a breadth-first search, which always finds the minimum number of steps.

What is the difference between Jump Game and Jump Game II?

The first problem asks whether the last index is reachable at all. This one guarantees it is reachable and asks for the fewest jumps, which requires tracking levels instead of only the farthest reach.

Can Jump Game II be solved with dynamic programming?

Yes, a DP table of minimum jumps per index works, but it is O(n²) in the worst case. The greedy window method gives the same answer in O(n) time.

Why is the time complexity O(n) when there are two nested loops?

The inner loop scans the current window, and consecutive windows never overlap. Across all iterations of the outer loop, each index is scanned at most once.