Jump Game II
MEDIUMDescription
You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].
Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where:
0 <= j <= nums[i]andi + j < n
Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].
Example 1:
Input: nums = [2,3,1,1,4] Output: 2 Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: nums = [2,3,0,1,4] Output: 2
Constraints:
1 <= nums.length <= 1040 <= nums[i] <= 1000- It's guaranteed that you can reach
nums[n - 1].
Approaches
Checkout 2 different approaches to solve Jump Game II. Click on different approaches to view the approach and algorithm in detail.
Dynamic Programming
This approach uses dynamic programming to solve the problem by breaking it down into smaller, overlapping subproblems. We build a dp array where dp[i] stores the minimum number of jumps required to reach the last index from index i. We compute this array from the end to the beginning.
Algorithm
- Create an integer array
dpof sizenand initialize its values to a large number to represent infinity. - Set
dp[n-1] = 0. - Loop from
i = n-2down to0:- For each
i, loop fromj = i + 1tomin(n - 1, i + nums[i]). - Update
dp[i]withmin(dp[i], 1 + dp[j]).
- For each
- The final answer is
dp[0].
This approach uses a bottom-up dynamic programming strategy. We define an array dp where dp[i] represents the minimum number of jumps needed to reach the last index n-1 from index i. Our ultimate goal is to find dp[0].
The logic is as follows:
- The base case is
dp[n-1] = 0, since we are already at the last index and require no jumps. - We then iterate backward from
i = n-2down to0. For each indexi, we consider all possible jumps we can make, which land us at an indexj(wherei < j <= i + nums[i]). - The minimum jumps from
iwould be 1 (for the current jump) plus the minimum jumps required from the landing positionj. - So, the recurrence relation is:
dp[i] = 1 + min(dp[j])for all reachablej. - We find this minimum by iterating through all possible
jfor a giveniand then updatedp[i].
import java.util.Arrays;
class Solution {
public int jump(int[] nums) {
int n = nums.length;
int[] dp = new int[n];
// Initialize dp array with a value representing infinity
Arrays.fill(dp, Integer.MAX_VALUE);
// Base case: 0 jumps needed from the last index to itself
dp[n - 1] = 0;
// Build the dp table from right to left
for (int i = n - 2; i >= 0; i--) {
int maxJump = i + nums[i];
for (int j = i + 1; j <= Math.min(n - 1, maxJump); j++) {
// If index j is reachable from the end
if (dp[j] != Integer.MAX_VALUE) {
// Update dp[i] with the minimum jumps
dp[i] = Math.min(dp[i], 1 + dp[j]);
}
}
}
return dp[0];
}
}
Complexity Analysis
Pros and Cons
- Conceptually simpler to understand than the greedy approach if you are familiar with DP.
- Correctly solves the problem by exploring all possibilities in a structured way.
- Not the most efficient solution. The time complexity can be high if the jump lengths are large, potentially leading to a 'Time Limit Exceeded' error on larger test cases.
Code Solutions
Checking out 4 solutions in different languages for Jump Game II. Click on different languages to view the code.
public class Solution { public int Jump ( int [] nums ) { int ans = 0 , mx = 0 , last = 0 ; for ( int i = 0 ; i < nums . Length - 1 ; ++ i ) { mx = Math . Max ( mx , i + nums [ i ]); if ( last == i ) { ++ ans ; last = mx ; } } return ans ; } }Video Solution
Watch the video walkthrough for Jump Game II
Similar Questions
5 related questions you might find useful
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.