// 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
最新推荐文章于 2018-04-24 16:03:56 发布