C++ using two list


  • -1
    W
    class MinStack {
    private:
    	list<int> min;
    	list<int> stk;
    
    public:
    	void push(int x) {
    		if(stk.empty())
    			min.push_back(x);
    		else if(x <= min.back())
    			min.push_back(x);
    		stk.push_back(x);
    	}
    
    	void pop() {
    		int a = stk.back();
    		if(a == min.back())
    			min.pop_back();
    		stk.pop_back();
    	}
    
    	int top() {
    		return stk.back();
    	}
    
    	int getMin() {
    		return min.back();
    	}
    };

Log in to reply
 

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