Posted on

Description

Submission

class MinStack {
    vector<int> stk;
public:
    /** initialize your data structure here. */
    MinStack() {
        
    }
    
    void push(int x) {
        int minimum = x;
        if(!stk.empty()) {
            minimum = min(stk.back(), x);
        }
        stk.push_back(x);
        stk.push_back(minimum);
    }
    
    void pop() {
        stk.pop_back();
        stk.pop_back();
    }
    
    int top() {
        return stk[stk.size()-2];
    }
    
    int getMin() {
        return stk.back();
    }
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(x);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->getMin();
 */

Leave a Reply

Your email address will not be published. Required fields are marked *