Jump Game II (LeetCode 45): Solution and Explanation
Each value nums[i] is the farthest you can jump forward from index i. Starting at index 0, find the minimum number of jumps needed to reach the last index, which is guaranteed to be reachable.
Watch it run step by step
Try your own input and write the code →Intuition
A natural first attempt is dynamic programming: let dp[i] be the fewest jumps to reach i, and for each index update every position it can reach. That works, but it costs O(n²) in the worst case because an index with a big jump value updates many cells. There is a lot of redundancy, since we only care about the minimum number of jumps, not about exact paths.
The better framing is breadth-first search. Index 0 is level zero. Every index you can reach in one jump forms level one, every index reachable in two jumps forms level two, and so on. The answer is the level that contains the last index. The key observation is that each level is a contiguous range of indices. If you can reach index 7 in two jumps, you can also reach every index between the end of level one and 7 in two jumps, because jumps can be shorter than the maximum.
So instead of a queue, you keep a window [l, r] for the current level. Scanning that window, you compute farthest, the largest i + nums[i] for any i inside it. The next level is then [r + 1, farthest]. Each time you move to the next window, you have spent one more jump. You stop as soon as the window's right edge reaches the last index.
Because the windows never overlap and each index is scanned exactly once across all levels, the whole thing is linear, and you never store more than a couple of integers.
Approach: Greedy BFS over windows
- Set
res = 0jumps and the current window tol = r = 0, which holds only the starting index. - While
r < len(nums) - 1, the last index has not been reached yet, so you need another jump. - Scan every index
ifromltorand trackfarthest = max(farthest, i + nums[i]). - Move to the next window by setting
l = r + 1andr = farthest. - Increment
resbecause entering the new window costs one jump. - When the loop exits,
rcovers the last index, so returnres.
Step-by-step example
nums = [2,3,1,1,4]
- Start with
l = r = 0andres = 0. The last index is 4, andr = 0is less than 4, so enter the loop. - Window [0, 0]: index 0 reaches 0 + 2 = 2, so
farthest = 2. The next window is [1, 2] andres = 1. r = 2is still less than 4. Window [1, 2]: index 1 reaches 1 + 3 = 4 and index 2 reaches 2 + 1 = 3, sofarthest = 4.- The next window is [3, 4] and
res = 2. - Now
r = 4is not less than 4, so the loop ends and the function returns 2 (jump from 0 to 1, then from 1 to 4).
Code: Python, Java, C++
1class Solution:
2 def jump(self, nums: List[int]) -> int:
3 res = 0
4 l = r = 0
5
6 while r < len(nums) - 1:
7 farthest = 0
8 for i in range(l, r + 1):
9 farthest = max(farthest, i + nums[i])
10 l = r + 1
11 r = farthest
12 res += 1
13 return res1class Solution {
2 public int jump(int[] nums) {
3 int res = 0;
4 int l = 0, r = 0;
5
6 while (r < nums.length - 1) {
7 int farthest = 0;
8 for (int i = l; i <= r; i++) {
9 farthest = Math.max(farthest, i + nums[i]);
10 }
11 l = r + 1;
12 r = farthest;
13 res++;
14 }
15 return res;
16 }
17}1class Solution {
2public:
3 int jump(vector<int>& nums) {
4 int res = 0;
5 int l = 0, r = 0;
6
7 while (r < nums.size() - 1) {
8 int farthest = 0;
9 for (int i = l; i <= r; i++) {
10 farthest = max(farthest, i + nums[i]);
11 }
12 l = r + 1;
13 r = farthest;
14 res++;
15 }
16 return res;
17 }
18};Time and space complexity
O(n)
The windows partition the indices into disjoint ranges, so the inner loop touches each index at most once over the entire run.
O(1)
Only the window bounds, the jump count and farthest are stored.
Other approaches
Bottom-up dynamic programming (time O(n²), space O(n))
Keep dp[i] as the minimum jumps to reach index i and, for each i, relax every index up to i + nums[i]. It is easy to reason about and generalizes to weighted variants, but it is quadratic when jump values are large.
Greedy with current end (time O(n), space O(1))
Iterate i from 0 to n - 2, updating a global farthest, and whenever i reaches currentEnd, count a jump and set currentEnd = farthest. It is the same BFS idea written as a single loop instead of nested window loops.
Common mistakes
- Looping while
r < len(nums)instead oflen(nums) - 1, which counts an extra jump once the last index is already inside the window. - Starting the next window at
l = rinstead ofr + 1, which rescans the previous level and can make the loop do extra work. - Greedily jumping to the index with the largest value inside the window instead of the one that reaches farthest. What matters is
i + nums[i], notnums[i]alone. - Forgetting the case of a single element array, where zero jumps are needed and the loop must not run at all.
Frequently asked questions
Why is the greedy approach for Jump Game II correct?
Every index reachable with k jumps forms a contiguous window, and the next window is everything up to the farthest reach from the current one. Counting windows until the last index is covered is exactly a breadth-first search, which always finds the minimum number of steps.
What is the difference between Jump Game and Jump Game II?
The first problem asks whether the last index is reachable at all. This one guarantees it is reachable and asks for the fewest jumps, which requires tracking levels instead of only the farthest reach.
Can Jump Game II be solved with dynamic programming?
Yes, a DP table of minimum jumps per index works, but it is O(n²) in the worst case. The greedy window method gives the same answer in O(n) time.
Why is the time complexity O(n) when there are two nested loops?
The inner loop scans the current window, and consecutive windows never overlap. Across all iterations of the outer loop, each index is scanned at most once.