Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.1 kB
64
Indexable
Never
class MinStack {
public:
    int stack[10000];
    int mini[10000];
    int topptr;
    int minptr;
    MinStack() {
        topptr=-1;
        minptr=-1;
    }
    
    void push(int val) {
        if(topptr>=10000) return;
        stack[++topptr]=val;
        if(minptr>=0){
            if(mini[minptr] > val){
                mini[++minptr]=val;
            }else{
                mini[minptr+1] = mini[minptr];
                minptr++;
            }
        }
        else{
            mini[++minptr] = val;
        }
    }
    
    void pop() {
        if(topptr<0) return;
        topptr-=1;
        minptr-=1;
    }
    
    int top() {
        if(topptr>=0)
            return stack[topptr];
        return -1;
    }
    
    int getMin() {
        if(minptr>=0)
            return mini[minptr];
        return -1;
    }
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(val);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->getMin();
 */