Memory Limit Exceed Error?


  • 0
    S

    The ques for getting minimum stack value in O(1) uses dynamic length vector. Since the array is resizable and the upper limit for the number of elements in the stack is not given.Its hard to solve this problem.
    It shows valid results for smaller number of inputs on codeblocks.

    class MinStack {

    private:
        std::vector<int> stack;
        std::vector<int> min;
    public:
    
        void push(int x)
        {
            stack.push_back(x);
            if(min.empty())
            {
                min.push_back(x);
            }
    
            else
            {
                int y = min.back();
                if(y >=x) min.push_back(x);
            }
        }
    
        void pop()
        {
            if(!stack.empty())
            {
    
                int x= stack.back();
                cout<<x<<"\n";
                stack.pop_back();
    
                //if(min.back() == x)cout<<"yes\n";
                //else cout<<"no\n";
    
                if(min.back()==x && !min.empty())
                {
                  min.pop_back();
                }
            }
    
        }
    
        int top()
        {
            int x = stack.back();
            cout<<x;
        }
    
        int getMin()
        {
            if(!min.empty())
            {
                int x= min.back();
                cout<<"min  "<<x<<"\n";
            }
        }
    };

Log in to reply
 

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