C++ 1-line solution for each method using stack<pair<int,int>> for pair(value, current min)


  • 0
    class MinStack {
    private: 
        stack<pair<int,int>> s; // pair of (value, current min)
    public:
        MinStack() { }
        void push(int x) { s.emplace(x, min(getMin(), x)); }
        void pop() { if (!s.empty()) s.pop(); }
        int top() { return s.top().first; }
        int getMin() { return s.empty()? INT_MAX : s.top().second; }
    };
    

Log in to reply
 

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