DSA AnimatorDSA animations
Jump Game II LC #45 Medium Greedy · BFS Levels
Problem

You are given a 0-indexed array of integers nums of length n. You start at nums[0]. Each element nums[i] represents the maximum jump length from that position. Return the minimum number of jumps to reach nums[n-1]. The test cases guarantee you can always reach the last index.

Example 1
Input: nums = [2,3,1,1,4]
Output: 2
Explanation: Jump 1 step from index 0 to index 1 (val 3), then jump 3 steps to the end. Min jumps = 2.
Example 2
Input: nums = [2,3,0,1,4]
Output: 2
Explanation: Jump to index 1 (val 3), then jump to index 4. Min jumps = 2.
Constraints: 1 ≤ nums.length ≤ 10⁴  |  0 ≤ nums[i] ≤ 1000  |  You can always reach the last index.
Try Examples
Custom:
Approach
Greedy BFS Levels — Minimum Jumps
Each jump covers a "level" of platforms. Scan the level, find the farthest reach, then jump to the next level.
Minimum Jumps
0
scanning level 0
curEnd: 0 farthest: 0
Jump Arena
Lv 0 (start)
Lv 1
Lv 2
Lv 3+
Variables
Index i
—
jumps
0
curEnd
0
farthest
0
Step Logic
Press ▶ Play or Next Step to begin the animation.
🎉
Ready
0 / 0
Select an example above and press Play.
Algorithm
1
Init jumps=0, curEnd=0, farthest=0
2
Loop i = 0 .. n-2: update farthest = max(farthest, i + nums[i])
3
When i == curEnd: jumps++, advance curEnd = farthest
4
Return jumps — the minimum number of jumps to reach last index
Time
O(n)
Space
O(1)
Why Greedy BFS Works

Think of each jump as a BFS level. Level 0 = just index 0. Level 1 = all platforms reachable in 1 jump. Level 2 = all reachable in 2 jumps, and so on. Instead of tracking exact paths, we scan the current level to find the farthest reachable index. When we exhaust the current level (i == curEnd), we commit exactly one jump and the new level spans curEnd+1 to farthest. This greedy frontier expansion always yields the minimum jumps in O(n).

How to solve Jump Game II

LeetCode 45 · Medium · Greedy · Pattern: Greedy BFS Levels

Why the obvious approach is slow

Trying every jump sequence is exponential, and dynamic programming is O(n²). A greedy scan does it in one pass.

The key idea

Think of it as levels: every index reachable with the same number of jumps forms one window. While scanning the current window, track the farthest index the next jump could reach. When you reach the end of the window, count a jump, and the next window ends at that farthest point.

Remember

Each jump is one window, like a level in BFS. Jump only when the current window runs out.

Run the animation above on your own input, or watch the 1:22 video explanation of Jump Game II with a dry run and Java code.

More Greedy problems