8ms Clean Java Solution


  • 0
    class MinStack {
        
        class Node {
            int x;
            int min;
            public Node(int x, int min) {this.x=x; this.min=min;}
        }
        
        private ArrayList<Node> arr = new ArrayList<Node>();
        
        public void push(int x) {
            if(arr.size()==0) {
                arr.add(new Node(x,x));
            } else {
                arr.add(new Node(x, Math.min(x,topNode().min)));
            }
        }
    
        public void pop() {
            arr.remove(arr.size()-1);
        }
        
        private Node topNode() {
            return arr.get(arr.size()-1);
        }
    
        public int top() {
            return topNode().x;
        }
    
        public int getMin() {
            return topNode().min;
        }
    }

Log in to reply
 

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