• + 0 comments

    A very easy solution in c++

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int main(){
        int n;
        cin>>n;
        stack<int> s;
        stack<int> maxStack;
        while(n--){
            int a,b;
            cin>>a;
            if(a==1){
                cin>>b;
            }
            if(a==1){
                s.push(b);
                if(maxStack.empty()){
                    maxStack.push(b);
                }
                else{
                    maxStack.push(max(maxStack.top(),b));
                }
            }
            else if(a==2){
                s.pop();
                maxStack.pop();
            }
            else{
                cout<<maxStack.top()<<endl;
            }
        }
    }