C++ beats 66% without using stack


  • 0
    A
    class MinStack {
    private: 
        int head = -1;
        int minhead = -1;
        int ms[8192];
        int mins[8192];
        
        
    public:
        /** initialize your data structure here. */
        MinStack() {
            head = -1;
            minhead = -1;
        }
        
        void push(int x) {
            head++;
            ms[head] = x;
            if(minhead < 0 || (minhead > -1 && ms[mins[minhead]] > x)){
                minhead++;
                mins[minhead] = head;
            }
        }
        
        void pop() {
            if(head == mins[minhead]) minhead--;
            head--;
        }
        
        int top() {
            return ms[head];
        }
        
        int getMin() {
            return ms[mins[minhead]];
        }
    };
    

Log in to reply
 

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