Java solution with 2 normal Stacks


  • 4
    N
    public class MinStack {
    
        Stack<Integer> st = new Stack<Integer>();
        Stack<Integer> pq = new Stack<Integer>();
        
        public MinStack() {
            
        }
        
        public void push(int x) {
            st.push(x);
            pq.push(Math.min(x,pq.size() > 0 ? pq.peek() : x));
        }
        
        public void pop() {
            st.pop();
            pq.pop();
        }
        
        public int top() {
            return st.peek();
        }
        
        public int getMin() {
            return pq.peek();
        }
    }
    

  • 0

    Brilliant!
    Please enter a longer post. Posts should contain at least 12 character(s).


Log in to reply
 

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