Skip to content

Instantly share code, notes, and snippets.

@manuchandel
Created October 2, 2015 15:49
Show Gist options
  • Save manuchandel/375588ceef4184c2ff19 to your computer and use it in GitHub Desktop.
Save manuchandel/375588ceef4184c2ff19 to your computer and use it in GitHub Desktop.
Minimum number of jumps to reach end
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