• + 0 comments
    vector<int> getMax(vector<string> operations) {
        vector<int> ans;
        int Max = INT_MIN;
        stack<int> st;
        int n=operations.size();
        for(int i=0;i<n;i++){
            string s = operations[i];
            int opern = stoi(s.substr(0,1));
            
            //perform operations
            if(opern == 1){ //push operation
                int val = stoi(s.substr(2));
                if(val>=Max){
                    st.push(Max);
                    Max=val;
                }
                st.push(val);
            }
            else if(opern == 2){ // pop operation
                if(Max == st.top()){
                    st.pop();
                    Max = st.top();
                }
                st.pop();
            }
            else if(opern == 3){
                ans.push_back(Max);
            }
        }
        return ans;
    }