clueless coding // TODO: be smarter

LeetCode Min Stack: Java Solution



TL;DR Code Solution


ss MinStack {

    Stack<Integer> stack = new Stack<Integer>();
    Stack<Integer> mins = new Stack<Integer>();

    /** initialize your data structure here. */
    public MinStack() {

    }

    public void push(int x) {
        stack.push(x);
        if (mins.isEmpty() || x < mins.peek()) {
            mins.push(x);
        }
        else {
            mins.push(mins.peek());
        }
    }

    public void pop() {
        stack.pop();
        mins.pop();
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return mins.peek();
    }