C++ 24ms solution


  • 0
    L
    MinStack()
    {
        t = 0;
    }
    void push(int x) {
        num.push_back(x);
        if(t == 0)
        {
            min.push_back(x);
        }
        else
        {
            long long m = min[t - 1] < x ? min[t - 1] : x;
            min.push_back(m); 
        }
        ++t;
    }
    
    void pop() {
        min.pop_back();
        num.pop_back();
        --t;
    }
    
    int top() {
        return num[t - 1];
    }
    
    int getMin() {
        return min[t -1];
    }
    vector<long long> min;
    vector<long long> num;
    int t;

Log in to reply
 

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