C++ beginner's solution


#1
int Solution::solve(vector<int> &A, int B) {
    int length = A.size();
    int curr=0;
    int lastone=0;
    int on=0;
    bool flag=false;
    
    while(curr<length)
    {
        flag=false;
        int i = min(curr+(B-1),length-1);
        while(i>=max(curr-B+1,lastone))
        {
            if(A[i]==1)
            {
                lastone=i;
                on++;
                curr=i+B;
                flag = true;
                break;
            }
            i--;
        }
        
        if(flag==false)
        {
            break;
        }
    }
    
    if(flag==false)
    {
        return -1;
    }
    else
    {
        return on;
    }
}