My concise cpp solution


  • 0
    C
    class MinStack {
    public:
        void push(int x) 
        {
            int lastmin = min.empty() ? ~(1<<31) : min.back();
            if(lastmin < x) min.push_back(lastmin);
            else            min.push_back(x);
            
            container.push_back(x);
        }
    
        void pop() 
        {
            container.pop_back();
            min.pop_back();
        }
    
        int top() 
        {
            return container.back();
        }
    
        int getMin() 
        {
            return min.back();
        }
        
        private:
            vector<int> container;
            vector<int> min;
    };

Log in to reply
 

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