Easy iterative approach in C++


#1

int Solution::canJump(vector &A)
{
int i=0,j,res=0;
if(A.size()==1)
{
return 1;
}
if(A[0]==0 && A.size()>1)
{
return 0;
}
while(i<A.size())
{
int count=0;
pair<int,int>p;
p.first=INT_MIN;
p.second=INT_MIN;
int z=i+A[i];
if(z>=A.size()-1)
{
return 1;
}
for(j=z;j>i;j–)
{
if(p.second<A[j]-count)
{
p.first=j;
p.second=A[j]-count;
}
count++;
}
if(p.second<=0)
{
return 0;
break;
}
i=p.first;
res++;
if(i==A.size()-1)
{
break;
}
}
if(res==0)
{
return 0;
}
else
{
return 1;
}
}