Get runtime error with prompt "Last executed input: push(-3),getMin"


  • 0
    N

    Why do I get runtime in the following code ?
    It shows "Last executed input: push(-3),getMin".
    I am using 2 stacks to resolve this problem and it works in my machine. here is my code :

    class MinStack{
    
    public:
        stack<int> mystack;
        stack<int> minval;
        
        void push(int x) {
            mystack.push(x);
            if(minval.top()>=x) minval.push(x);
        }
    
        void pop() {
            if(!mystack.empty()){
    
                if(mystack.top() == minval.top()) minval.pop();
                mystack.pop();
            }
    
        }
    
        int top() {
            return mystack.empty() ? 0 : mystack.top();
        }
    
        int getMin() {
            return mystack.empty() ? 0 : minval.top();
        }
    
    };

  • 0
    M

    What was the issue? I am also hitting on the same issue.


Log in to reply
 

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