C++11 5 lines solution


  • 1
    N
    struct MinStack {
        vector<pair<int, int>> st;
        
        void push(int x) {
            st.emplace_back(x, min(x, st.empty() ? x : st.back().second));
        }
        
        void pop() {
            st.pop_back();
        }
        
        int top() {
            return st.back().first;
        }
        
        int getMin() {
            return st.back().second;
        }
    };
    
    

Log in to reply
 

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