3.2 Stack min

Using extra stack to track minimum element.

class MinStack {
public:
    stack<int> s1;
    stack<int> s2;
    void push(int x) {
        s1.push(x);
        if(s2.empty() || x<= getMin()) s2.push(x);
    }

    void pop() {
        if (s1.top() == getMin()) s2.pop();
        s1.pop();
    }

    int top() {
        return s1.top();
    }

    int getMin() {
        return s2.top();
    }
};

你可能感兴趣的:(stack)