Last active
October 8, 2018 23:43
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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