Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.

Try one problem
0
Problem

Jump Game II

LeetCode #45Medium
Minimum jumps — greedy BFS by levels

Given an array where each element is the maximum jump length from that position, return the minimum number of jumps needed to reach the last index. It is guaranteed the last index is reachable.

Asked atAmazonGoogleMeta
step 1 / 9
2
3
1
1
4
[0][1][2][3][4]
Greedy · layer-by-layer farthest reach
1given nums
2jumps ← 0; curEnd ← 0; farthest ← 0
3for i ← 0 to n − 2:
4 farthest = max(farthest, i + nums[i])
5 if i == curEnd:
6 jumps++; curEnd ← farthest
7 if curEnd ≥ n − 1: break
8return jumps
state
  • n5
  • last4
  • nums[2, 3, 1, 1, 4]

line 1Jump Game II: we KNOW we can reach the end — find the MINIMUM number of jumps. Think of it as breadth-first search by layers. With 0 jumps you can stand only on index 0; with 1 jump you can reach everything index 0 unlocks; with 2 jumps everything THOSE indices unlock, and so on. We count how many layers it takes to cover the last index.