An accepted C++ solution


  • 0
    M
    class MinStack {
    public:
        MinStack()
        {
            sp=-1;
            mp=-1;
        }
        
        void push(int x) {
            sp++;
            stk[sp]=x;
            if(mp==-1||min[mp]>=x)
            {
                mp++;
                min[mp]=x;
            }
        }
    
        void pop() {
            if(sp<0)
                return;
            if(min[mp]==stk[sp])
                mp--;
            sp--;
        }
    
        int top() {
            if(sp>-1)
                return stk[sp];
            else
                return INT_MIN;
        }
    
        int getMin() {
            if(mp>-1)
                return min[mp];
            else 
                return INT_MIN;
        }
    private:
        int sp;
        int stk[100000];
        int mp;
        int min[100000];
    };

Log in to reply
 

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