Java solution


  • 0
    J
    public class MinStack {
        Stack<int[]> stack;
    
        /** initialize your data structure here. */
        public MinStack() {
            stack = new Stack<>();
        }
        
        public void push(int x) {
            if (stack.isEmpty()) stack.push(new int[]{x, x});
            else stack.push(new int[]{x, Math.min(x, (stack.peek())[1])});
        }
        
        public void pop() {
            stack.pop();
        }
        
        public int top() {
            return (stack.peek())[0];
        }
        
        public int getMin() {
            return (stack.peek())[1];
        }
    }
    

Log in to reply
 

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