https://codility.com/demo/take-sample-test/fish
一开始习惯性使用单调栈,后来发现一个普通栈就可以了。
#include <stack> using namespace std; int solution(vector<int> &A, vector<int> &B) { int size = A.size(); int deadNum = 0; stack<int> stk; // downstream for (int i = 0; i < size; i++) { if (B[i] == 0) { bool done = false; while (not done) { if (!stk.empty()) { int top = stk.top(); if (A[top] > A[i]) { deadNum++; done = true;; } else { deadNum++; stk.pop(); } } else { done = true; } } } else { // B[i] == 1 stk.push(i); } } return (size - deadNum); }