Jump Game
Level - Medium
Statement
given an array of non-negative integers
nums
, you are initially positioned at thefirst index
of the array, at each indexnums[i]
represents the maximum jump length you can take from the current indexi
. You have to return minimum number of jumps needed to reach at the endnums[n - 1]
.
Example 1
Input
: nums = [2,3,0,1,4]
Output
: 2
Explaination
: in above nums
this jump route 0th index -> 1th index -> 4th index
will give you minimum jumps to reach at the end. so in total there are 2
jumps needed.
Constraints
- 1 <= nums.length <= 104
- 0 <= nums[i] <= 1000
You can try this at:
Brute Force Approach
- first the approach which came in our mind is to try out all possible ways in which we can reach from
index=0
toindex=n-1
. - this can only be possible with the help of recursion.
- there can be multiple ways to reach from
index=0
toindex=n-1
. - we have to select the way in which we need
minimum jumps
.
Recursive Tree
Recursion Dry Run
- Java
- Python
- CPP
class Solution {
// given in constraints
int MAX_JUMPS_COUNT = 10000;
public int dfs(int[] nums,int index){
// index beyond n-1 is not acceptable so return MAX_JUMP_COUNT
if(index>=nums.length){
return MAX_JUMPS_COUNT;
}
// we have reached to last index
if(index==nums.length-1){
return 0;
}
int min_jump = MAX_JUMPS_COUNT;
for(int i=1;i<=nums[index];i++){
int jump_count = 1+dfs(nums,index+i);
min_jump = Math.min(min_jump,jump_count);
}
return min_jump;
}
public int jump(int[] nums) {
return dfs(nums,0);
}
}
class Solution:
MAX_JUMPS_COUNT = 10000
def dfs(self, nums, index):
if index >= len(nums):
return self.MAX_JUMPS_COUNT
if index == len(nums) - 1:
return 0
min_jump = self.MAX_JUMPS_COUNT
for i in range(1, nums[index] + 1):
jump_count = 1 + self.dfs(nums, index + i)
min_jump = min(min_jump, jump_count)
return min_jump
def jump(self, nums):
return self.dfs(nums, 0)
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
class Solution {
public:
int MAX_JUMPS_COUNT = 10000;
int dfs(vector<int>& nums, int index) {
if (index >= nums.size()) {
return MAX_JUMPS_COUNT;
}
if (index == nums.size() - 1) {
return 0;
}
int min_jump = MAX_JUMPS_COUNT;
for (int i = 1; i <= nums[index]; i++) {
int jump_count = 1 + dfs(nums, index + i);
min_jump = min(min_jump, jump_count);
}
return min_jump;
}
int jump(vector<int>& nums) {
return dfs(nums, 0);
}
};
Complexity
Time Complexity:
O(N^N)
Space Complexity:
O(N)
Optimize this Approach by Memoization
Memoization
is the technique to save the previous results
Explaination
- Java
- Python
- CPP
class Solution {
// given in constraints
int MAX_JUMPS_COUNT = 10000;
public int dfs(int[] nums,int index,int[] memo){
if(index>=nums.length){
return MAX_JUMPS_COUNT;
}
if(index==nums.length-1){
return 0;
}
if(memo[index]!=-1)return memo[index];
int min_jump = MAX_JUMPS_COUNT;
for(int i=1;i<=nums[index];i++){
int jump_count = 1+dfs(nums,index+i,memo);
min_jump = Math.min(min_jump,jump_count);
}
memo[index] = min_jump;
return min_jump;
}
public int jump(int[] nums) {
int[] memo = new int[nums.length];
Arrays.fill(memo,-1);
return dfs(nums,0,memo);
}
}
class Solution:
MAX_JUMPS_COUNT = 10000
def dfs(self, nums, index, memo):
if index >= len(nums):
return self.MAX_JUMPS_COUNT
if index == len(nums) - 1:
return 0
if memo[index] != -1:
return memo[index]
min_jump = self.MAX_JUMPS_COUNT
for i in range(1, nums[index] + 1):
jump_count = 1 + self.dfs(nums, index + i, memo)
min_jump = min(min_jump, jump_count)
memo[index] = min_jump
return min_jump
def jump(self, nums):
memo = [-1] * len(nums)
return self.dfs(nums, 0, memo)
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
class Solution {
public:
int MAX_JUMPS_COUNT = 10000;
int dfs(vector<int>& nums, int index, vector<int>& memo) {
if (index >= nums.size()) {
return MAX_JUMPS_COUNT;
}
if (index == nums.size() - 1) {
return 0;
}
if (memo[index] != -1) {
return memo[index];
}
int min_jump = MAX_JUMPS_COUNT;
for (int i = 1; i <= nums[index]; i++) {
int jump_count = 1 + dfs(nums, index + i, memo);
min_jump = min(min_jump, jump_count);
}
memo[index] = min_jump;
return min_jump;
}
int jump(vector<int>& nums) {
vector<int> memo(nums.size(), -1);
return dfs(nums, 0, memo);
}
};
Complexity
Time Complexity:
O(N*N)
Space Complexity:
O(N)
in an interview you don't need to tell Iterative approach
but if you want to grasp DP Concepts Iterative Method will help you to better understand the question
Iterative Approach/Bottom Up Approach
Explaination
iterative / bottom up
approach , create a1D
DP arraydp[]
dp[i]
represents the min jump need fromi
ton-1
indexdp[n-1] = 0
as it is thelast
index- start from
n-2
index and at each index:- try all jumps from gap = 1 to gap = nums[ index ]
dp[index] = min jump from index to n-index
- Java
- CPP
- Python
class Solution {
// given in constraints
int MAX_JUMPS_COUNT = 10000;
public int jump(int[] nums) {
int n = nums.length;
int[] dp = new int[n];
for(int i=n-2;i>=0;i--){
int min_jump = MAX_JUMPS_COUNT;
for(int gap=1;gap<=nums[i];gap++){
if(gap+i < nums.length)
min_jump = Math.min(min_jump,1+dp[i+gap]);
}
dp[i] = min_jump;
}
return dp[0];
}
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
class Solution {
public:
int MAX_JUMPS_COUNT = 10000;
int dfs(vector<int>& nums, int index, vector<int>& memo) {
if (index >= nums.size()) {
return MAX_JUMPS_COUNT;
}
if (index == nums.size() - 1) {
return 0;
}
if (memo[index] != -1) {
return memo[index];
}
int min_jump = MAX_JUMPS_COUNT;
for (int i = 1; i <= nums[index]; i++) {
int jump_count = 1 + dfs(nums, index + i, memo);
min_jump = min(min_jump, jump_count);
}
memo[index] = min_jump;
return min_jump;
}
int jump(vector<int>& nums) {
vector<int> memo(nums.size(), -1);
return dfs(nums, 0, memo);
}
};
class Solution:
MAX_JUMPS_COUNT = 10000
def jump(self, nums):
n = len(nums)
dp = [0] * n
for i in range(n - 2, -1, -1):
min_jump = self.MAX_JUMPS_COUNT
for gap in range(1, nums[i] + 1):
if gap + i < len(nums):
min_jump = min(min_jump, 1 + dp[i + gap])
dp[i] = min_jump
return dp[0]
Complexity
Time Complexity:
O(N*N)
Space Complexity:
O(N)
N
denotes nums
array length