[codility]Fish

// you can also use includes, for example:
// #include <algorithm>
#include <stack>
int solution(vector<int> &A, vector<int> &B) {
    // write your code in C++98
    //...keep record of  downstream fishs in stack
    stack<int> downFishStack;
    int totalAliveCnt = 0;
    for(int i = 0; i < B.size(); ++i)
    {
        if(B[i] == 1) downFishStack.push(i);
        else
        {
            while(!downFishStack.empty())
            {
                int topDownFishIdx = downFishStack.top();
                if(A[topDownFishIdx] > A[i]) break;
                else downFishStack.pop();
            }
            
            if(downFishStack.empty()) totalAliveCnt++;
        }
    }
    //...add remaining downstream fish
    totalAliveCnt += downFishStack.size();
    //...return result
    return totalAliveCnt;
}

你可能感兴趣的:([codility]Fish)