C++ code using single stack


  • 0
    S

    class MinStack {
    private:
    stack<pair<int, int>> s;
    public:
    /** initialize your data structure here. */
    MinStack() {

    }
    
    void push(int x) {
        s.push(s.empty()?make_pair(x,x):make_pair(x,min(s.top().second,x)));
    }
    
    void pop() {
        s.pop();
    }
    
    int top() {
        return s.top().first;
    }
    
    int getMin() {
        return s.top().second;
    }
    

    };


Log in to reply
 

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