• + 0 comments

    C++ solution

    vector<int> getMax(vector<string> operations) {
    int a = 0;
    
    stack<int> mem;
    stack<int> max;
    vector<int> res;
    
    for(auto i : operations){
        if(i.size() > 1){
            a = stoi(i.substr(2));
            mem.push(a);
            if(max.empty() || max.top() <= a){
                max.push(a);
            }
        }
        else{
            if(i == "2"){
                if(mem.top() == max.top()) max.pop();
                mem.pop();
            }
            if(i == "3"){
                res.push_back(max.top());
            }
        }
    }
    return res;
    }