Codility PassingCars

int solution(vector<int> &A) {
    // write your code in C++14 (g++ 6.2.0)
    int res = 0;
    int total1 = 0;
    int isFirstLoop = true;
    int temp = 0;
    for(int i = 0; i < A.size();i++)
    {
        
        if(A[i] == 0 )
        {
            if(isFirstLoop)
            {
                isFirstLoop = false;
                for(int j = i; j < A.size(); j ++)
                {
                    if(A[j] == 1)
                    {
                        total1++;
                        res++;
                    }
                }
            }
            else
            {
                res+= (total1-temp);
            }
            //temp = 0;
        }
        else if(A[i] == 1)
        {
            temp++;
        }
    }
    
    return res;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章