Simple way to iterate


#1

int Solution::solve(vector &A, int B) {

int jump = B-1,counter=0,n=A.size(),i=0;
while(i<n)
{
    int rightmost_light=-1;
    for(int j=max(0,i-jump);j<=min(n-1,i+jump);j++)
    {
        if(A[j]==1) rightmost_light=j;
    }
    if(rightmost_light==-1) return -1;
    counter++;
    i=rightmost_light+B+1;

}
return counter;

}