Jump Game II
Medium
Topics
Given an array nums where nums[i] is the maximum jump length from index i, return the minimum number of jumps to reach the last index. You can assume the last index is always reachable.
Example 1
end of each jump level
This is a shortest path in disguise, and the levels are what to count: from everything reachable in
k jumps, the next level is everything reachable in k+1.Cells are the array values; the small numbers underneath are indices.
Input: nums = [2,3,1,1,4] Output: 2 Explanation: Jump 1 step to index 1, then 3 steps to the end.
Example 2
end of each jump level
Input: nums = [2,3,0,1,4] Output: 2
Constraints
- 1 <= nums.length <= 10^4
- 0 <= nums[i] <= 1000
- The last index is reachable.
Editorial — intuition, approach, complexity and pitfalls
Intuition
Think of it as BFS over "levels." Everything reachable within j jumps forms a contiguous range. When you exhaust the current range you must take another jump, extending to the farthest index seen so far.
Approach
- Track
curEnd(edge of the current jump range) andfarthest(best reach seen). - At each i update farthest; when i hits curEnd, increment jumps and set curEnd = farthest.
- Stop before the last index — you never need to jump from it.
Dry Run
nums = [2, 3, 1, 1, 4]
Think of it as BFS over levels. `cur_end` is the last index reachable with the
jumps taken so far; `far` is the furthest reachable with one more.
i=0 nums[0]=2 far extends to 2 i == cur_end, so JUMP 1; frontier becomes 2
i=1 nums[1]=3 far extends to 4 inside the current jump (frontier 2)
i=2 nums[2]=1 far stays 4 i == cur_end, so JUMP 2; frontier becomes 4
i=3 nums[3]=1 far stays 4 inside the current jump
answer 2 -- index 0 to 1, then 1 to 4
No jump is ever chosen explicitly. The count increments on reaching the boundary
of the current level, and `far` has by then absorbed the best reach of every index
in that level -- which is why this is one pass and not a search.
The loop stops at n - 2. Reaching the last index means you have arrived, and
letting the loop touch it would increment on the final boundary: this input then
returns 3 instead of 2, a single-element [0] returns 1 instead of 0, and [1,2]
returns 2 instead of 1.
The problem guarantees the end is reachable, which is why no "impossible" branch
exists. Without that guarantee, far == i with i < n - 1 would be the stall to
detect.Complexity
- Time — O(n): a single sweep updating two counters.
- Space — O(1): constant extra space.
Common Pitfalls
- Loop only to n-2; counting a jump from the last index would over-count.
Full Solution — all languages
class Solution:
def jump(self, nums):
jumps = cur_end = far = 0
for i in range(len(nums) - 1):
if i + nums[i] > far:
far = i + nums[i]
if i == cur_end:
jumps += 1
cur_end = far
return jumpsimport java.util.*;
class Solution {
public int jump(int[] nums) {
int jumps = 0, curEnd = 0, far = 0;
for (int i = 0; i < nums.length - 1; i++) {
if (i + nums[i] > far) far = i + nums[i];
if (i == curEnd) {
jumps++;
curEnd = far;
}
}
return jumps;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int jump(vector<int>& nums) {
int jumps = 0, curEnd = 0, far = 0;
int n = nums.size();
for (int i = 0; i < n - 1; i++) {
if (i + nums[i] > far) far = i + nums[i];
if (i == curEnd) {
jumps++;
curEnd = far;
}
}
return jumps;
}
};/**
* @param {number[]} nums
* @return {number}
*/ var jump = function(nums) {
let jumps = 0, curEnd = 0, far = 0;
for (let i = 0; i < nums.length - 1; i++) {
if (i + nums[i] > far) far = i + nums[i];
if (i === curEnd) {
jumps++;
curEnd = far;
}
}
return jumps;
};