Created
October 2, 2015 15:49
-
-
Save manuchandel/375588ceef4184c2ff19 to your computer and use it in GitHub Desktop.
Minimum number of jumps to reach end
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 minJump(vector<int> &A) { | |
int i,n=A.size(); | |
if(n==1) | |
return 0; | |
int steps=1; | |
int currentMaxReachable=A[0]; | |
int maxReachable=A[0]; | |
for(i=0;i<=maxReachable;i++){ | |
if(i==n-1) | |
return steps; | |
currentMaxReachable=max(currentMaxReachable,i+A[i]); | |
if(i==maxReachable){ | |
if(currentMaxReachable<=i) | |
return -1; | |
maxReachable=currentMaxReachable; | |
steps++; | |
} | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment