C++ using std::list will as inner container will result to Memory Limit Exceeded


  • 0
    B

    class MinStack
    {
    public:
    void push(int x) {
    elements.push_back(x);
    if(current_min.empty()){
    current_min.push_back(x);
    }
    else if(x <= current_min.back() ){
    current_min.push_back(x);
    }
    }

    void pop() {
        int t = elements.back();
        elements.pop_back();
        if(t == current_min.back()){
            current_min.pop_back();
        }
    }
    
    int top() {
        return elements.back();
    }
    
    int getMin() {
        return current_min.back();
    }
    private:
    list<int> elements;
    list<int> current_min;
    

    };

    what I didn't understand is that std::list have better efficient of space than std::vector, while std::vector(the actual implement of std::stack) may require double size of space than they actually need.


Log in to reply
 

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