C++ solution beating Runtime: 48 ms


  • 0
    K

    class MinStack {
    public:
    stack<int> s;
    stack<int> s1;
    int min;
    /** initialize your data structure here. */
    MinStack() {
    min=INT_MAX;
    }

    void push(int x) {
       //std::cout<<min;
       if(min>x)
       {
           min=x;
       }
       s.push(x);
       s1.push(min);
    }
    
    void pop() {
        
        s1.pop();
        s.pop();
        if(s.empty())
        min=INT_MAX;
        else
        min=s1.top();
    }
    
    int top() {
        return s.top();
        //return 1;
    }
    
    int getMin() {
        return s1.top();
        //return 1;
    }
    

    };


Log in to reply
 

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