Skip to content

Jump Game II

Find the fewest jumps from the first index to the last by scanning the array in BFS-like windows.

MediumGreedy
Solve on LeetCode

The problem

You stand on index 0 of an array nums, and nums[i] is the longest jump you can make from index i. Return the fewest jumps needed to reach the last index. You can assume the last index is always reachable.

Input nums = [3, 1, 4, 1, 1, 1, 2]

Output 2

Jump to index 2, then 4 more to index 6. Jumping as far as possible first, to index 3, would need 4 jumps.

Input nums = [1, 1, 1, 1]

Output 3

Every index lets you move only one step.

Key constraint: There are up to 10,000 indexes, so an O(n^2) DP would pass, but a single greedy pass is enough.

How to spot the pattern

  • You want the fewest moves, like a shortest path where every jump costs 1.
  • The indexes reachable in k jumps form one unbroken window, so the array can be processed window by window.

The idea

This is BFS without a queue. The indexes you can reach with exactly k jumps form a window, and the next window ends at the largest i + nums[i] over the current one. So scan from left to right keeping farthest; each time you reach the end of the current window you must take one more jump, and the new window ends at farthest.

Brute force: Try every jump sequence recursively, which takes exponential time; even a DP over the indexes takes O(n^2).

Step by step

  1. 1Start with jumps = 0, windowEnd = 0 and farthest = 0.
  2. 2Scan the indexes from 0 to n - 2, updating farthest = max(farthest, i + nums[i]).
  3. 3When i equals windowEnd, every index reachable with jumps jumps has been scanned, so add a jump and set windowEnd = farthest.
  4. 4Return jumps after the scan.

See the pattern move

The greedy pattern on a small example. Step through it, or press play.

Code

class Solution:
    def jump(self, nums: List[int]) -> int:
        jumps = 0
        window_end = 0  # last index reachable with `jumps` jumps
        farthest = 0  # farthest index reachable with one more jump
        for i in range(len(nums) - 1):  # no jump is needed from the last index
            farthest = max(farthest, i + nums[i])
            if i == window_end:
                # every index this many jumps can reach has been scanned: take one more jump
                jumps += 1
                window_end = farthest
        return jumps

Complexity

Time

O(n)

Space

O(1)

Each index is scanned once.

Watch out for

  • Scanning the last index too: if i meets windowEnd there, you count a jump you never take.
  • Jumping to the farthest landing spot each time, instead of the spot whose own jump reaches farthest.

Follow-up questions

What if the end might be unreachable?

When i reaches windowEnd and farthest is still equal to it, no jump gets you any further, so return -1.

Why is this BFS?

Each window is one BFS level: the indexes at distance k. Because levels are contiguous, scanning one level to find where the next ends replaces the queue.

In Hinglish

Har jump ek BFS level jaisa hai. Current window scan karte hue sabse door ka index note karo; window khatam hote hi jump badhao aur nayi window wahin tak le jao.

Related problems