MEMORY LIMIT EXCEED ERROR


  • 0
    S

    My solution uses vector array for dynamic size of stack but since the judge doesn't give description of why MLE is there as well as no upper and lower bounds on the input is mentioned,I am unable to figure out the problem. In my codeblocks compiler the output runs fine.Since I am new to leetcode please help.

    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";
            }
        }
    };``

  • 0
    S

    i guess the judge is checking memory cost as well as time cost it mentions in question explicitly. so the question has a problem itself (not draw out memory cost limitation). your answer is correct.


  • 0
    Z

    I guess that LeetCode's runtime environment allows a small limited amount of memory for this code.

    Not allowing the dynamic stack to insert more than a fixed number(around 68000) of elements gets the code accepted.

    Here is my code:-

    class MinStack {
    public:
        stack<pair<int,int> >s;
        int min;
        
        #define getmin(a,b)  ((a)<(b) ? (a) : (b))
       
        void push(int x) 
        {
            if(s.size()>68000)
                return;
            if(s.empty())
                min=x;`enter code here`
            else
                min=getmin(s.top().second,x);
                
            s.push(make_pair(x,min));   
            
        }
    
        void pop() 
        {
            if(!s.empty())
                s.pop();   
        }
    
        int top() 
        {
            if(!s.empty())
                return s.top().first;
            return 0;
        }
    
        int getMin() 
        {
            if(!s.empty())
                return s.top().second;  
            return 0;
        }
        
    };

Log in to reply
 

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