Why this doesn't work?


  • 0
    D

    class MinStack
    {
    private Stack<Integer> s1;
    private Stack<Integer> s2;

    public MinStack()
    {
    	s1=new Stack<>();
    	s2=new Stack<>();
    }
    
    public void push(int x)
    {
    	s1.push(x);
    	if (s2.empty()||x<=s2.peek())
    		s2.push(x);
    }
    
    public void pop()
    {
    	if (s1.peek()==s2.peek())
    	{
    		s2.pop();
    	}
    	s1.pop();
    }
    
    public int top()
    {
    	return s1.peek();
    }
    
    public int getMin()
    {
    	return s2.peek();
    }
    

    }
    Input: push(512),push(-1024),push(-1024),push(512),pop,getMin,pop,getMin,pop,getMin
    Output: [-1024,-1024,-1024]
    Expected: [-1024,-1024,512]


Log in to reply
 

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