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.
nums = [2,3,1,1,4]2nums = [2,3,0,1,4]2jumps=0, curEnd=0, farthest=0i = 0 .. n-2: update farthest = max(farthest, i + nums[i])i == curEnd: jumps++, advance curEnd = farthestjumps — the minimum number of jumps to reach last indexThink 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).
Trying every jump sequence is exponential, and dynamic programming is O(n²). A greedy scan does it in one pass.
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.
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.