Very popular question....(Easy solution!)


#1

int Solution::canJump(vector &A) {
int i=0;
int iprev=0;
while(i<A.size()){
if(i==A.size()-1){
return 1;
}
if(A[i]>0){
i=i+A[i];
}
else{
int c=i;
if(i==iprev){
return 0;
}
else{
iprev=i;
}
while(A[c]==0 || (c>0 && A[c]<=i-c)){
c–;
}
i=c;
if(c==0 || c==iprev){
return 0;
}
i=i+A[c];
}
//return 1;
}
return 1;
}