Just replacing two stack with two double ended queues saves 8ms. Total is 48ms, better than 96%


  • 0
    class MinStack {
    public:
        /** initialize your data structure here. */
        deque<int>dq;
        deque<int> Min;
        
        MinStack() {        
        }
       
        void push(int x) {
            Min.push_front(Min.empty() ? x : min(Min.front(), x));            
            dq.push_front(x);
        }
        
        void pop() {
            dq.pop_front();
            Min.pop_front();
        }
        
        int top() {
            return dq.front();
        }
        
        int getMin() {
            return Min.front();
        }
    };
    

Log in to reply
 

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