class Solution {
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        int n = pushV.size();
        int oi = 0, ui = 0; // index in popV, pushV
        vector<int> stack;
        while (ui<n){
            stack.push_back(pushV[ui]);
            while (oi<n && !stack.empty() && stack.back()==popV[oi]) {
                stack.pop_back();
                oi++;
            }
            ui++;
        }
        return stack.empty();
    }
};