C++ vector, 84%


  • 0
    7
    class MinStack {
        vector<int> e;//elements
        vector<int> id;//indexofmin
        int s;//size
        int m;//min, not necessary
    public:
        /** initialize your data structure here. */
        MinStack() {
            e=*new vector<int>();e.push_back(INT_MAX);
            id=*new vector<int>();id.push_back(0);
            s=0;
            m=INT_MAX;
        }
        
        void push(int x) {
            e.push_back(x);s++;
            if(x<m){m=x;id.push_back(s);}
            else id.push_back(id.back());
        }
        
        void pop() {
            e.pop_back();s--;
            id.pop_back();
            m=e[id.back()];
        }
        
        int top() {
            return e[s];
        }
        
        int getMin() {
            return m;
        }
    };
    

Log in to reply
 

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