Skip to content
AI360Xpert
Beta
Difficulty: MediumGreedy

Jump Game II

Problem in Plain English

Given an array where each element caps how far you can jump forward, find the fewest jumps needed to travel from the first index to the last one. Reaching the end is always possible.

Problem Statement

You are given a 0-indexed array nums where nums[i] is the maximum forward jump length from index i. Starting at index 0, reach index n - 1 in as few jumps as possible.

The test cases guarantee the last index is reachable. For nums = [2, 3, 1, 1, 4] the answer is 2: jump from index 0 to 1, then from 1 to 4.

Constraints

  • 1 <= nums.length <= 10^4
  • 0 <= nums[i] <= 1000
  • Reaching the last index is always possible.

Examples

Example 1

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

The path 0 -> 1 -> 4 uses two jumps. One jump cannot reach index 4 because nums[0] = 2, so two is the minimum.

Example 2

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

The zero at index 2 is a trap for a path landing there, but 0 -> 1 -> 4 skips it. Index 4 lies beyond the first jump range [1, 2], proving that the two-jump path is optimal.

Example 3

Input
nums = [0]
Output
0

The first index is already the last index. No move is needed, even though its jump length is zero.

Intuition

A jump length is a maximum, so landing closer can set up a better next jump. In Example 1, jumping straight from index 0 to 2 needs three jumps overall, while landing at 1 reaches index 4 in two. Instead of committing to one landing, keep the whole group of indices reachable with the same minimum jump count. After one jump that group is [1, 2]; either member may launch the next jump. The best reach among them is 1 + 3 = 4, so two jumps suffice.

Approaches

Brute Force DP

Solution Details

Reveal Brute Force DP: intuition, complexity, and code

Hints

Hint 1
Group indices by how many jumps it takes to reach them rather than tracking single paths.
Hint 2
Ask how far the whole current group can collectively reach in one more jump.
Hint 3
Slide a window from each reach frontier to the farthest point it attains and count the slides.

Edge Cases

  • Single element [0], where zero jumps already stand on the last index.
  • First element reaching the end directly, as in [5, 1, 1, 1], for an answer of 1.
  • Zeros inside the array that the window must leap over, as in [2, 3, 0, 1, 4].
  • All ones force n - 1 jumps; every window advances by exactly one index.
  • A frontier beyond the final index, as in [1000, 0], still gives one jump and must not trigger an out-of-range scan.

Common Mistakes and Interview Tips

  • Always landing as far as possible: Example 1 goes 0 -> 2 -> 3 -> 4 in three jumps, while 0 -> 1 -> 4 needs two.
  • Stopping the loop one window early and undercounting the final jump onto the last index.
  • Recomputing reachability with overlapping windows, which repeats scans and loses the linear bound.
  • Incrementing the jump counter for each scanned index instead of once per completed layer.
  • Applying this loop to an unreachable input without a progress check. That is outside the contract; a stuck frontier would loop forever.
  • In Java DP, adding one to Integer.MAX_VALUE before checking reachability causes overflow and a false minimum.

Key Takeaway

When every edge costs one, expand the reachable frontier wave by wave instead of pricing individual paths.