Using only vectors in C++. Easy to under stand.


  • 0
    N
    class MinStack {
    public:
        vector<long long int>v,k;
        long long  mini=1000000000000;
        void push(long long int x) {
            if(x<=mini)
            {
                mini=x;
                k.push_back(x);
            }
            v.push_back(x);
        }
    
        void pop() {
            if(v[v.size()-1]==k[k.size()-1])
            {
                k.pop_back();
                if(k.size()!=0)mini=k[k.size()-1];
                else mini=1000000000000;
            }
            v.pop_back();
        }
    
        long long top() {
            return v[v.size()-1];
        }
    
        long long getMin() {
            return mini;
        }
    };

Log in to reply
 

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