6 lines concise and easy understand c++ solution


  • 11
    A
    class MinStack {
    public:
        void push(int x) {
            int minnum = st.empty() ? x : min(st.top().second, x);
            st.push(make_pair(x, minnum));
        }
    
        void pop() {
            st.pop();
        }
    
        int top() {
            return st.top().first;
        }
    
        int getMin() {
            return st.top().second;
        }
    private:
        stack<pair<int, int>> st;
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.