Skip to content

Instantly share code, notes, and snippets.

@shubhampateliitm
Last active October 8, 2018 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shubhampateliitm/a7fb11ef14eef787d7263c3448527575 to your computer and use it in GitHub Desktop.
Save shubhampateliitm/a7fb11ef14eef787d7263c3448527575 to your computer and use it in GitHub Desktop.
https://www.interviewbit.com/problems/min-jumps-array/ Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. Example : Given array A = [2,3,1,1,4] Th…
int Solution::jump(vector<int> &A) {
int lastIndex = A.size()-1;
int sz = A.size();
int i = 0;
int checkUpTo = i+A[i];
int jumpCount = 1;
while(i<=checkUpTo){
int locMax = i + A[i];
while(i<=checkUpTo){
if(i>=lastIndex){
return jumpCount;
}
locMax = max(locMax,i+A[i]);
i++;
}
jumpCount++;
checkUpTo = locMax;
if(locMax>=lastIndex){
return jumpCount;
}
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment