Clean and short two Stack Java Solution beat 85%


  • 0
    F

    public class MinStack {
    private Stack<Integer> st = new Stack<Integer>();
    private Stack<Integer> m = new Stack<Integer>();

    public void push(int x) {
    	st.push(x);
    	if (m.isEmpty() || x <= m.peek())
    		m.push(x);
    }
    
    public void pop() {
    	if(st.pop().equals(m.peek()))
    		m.pop();
    	}
    
    public int top() {
    	return st.peek();
    }
    
    public int getMin() {
    	return m.peek();
    }
    

    }


Log in to reply
 

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