Min Stack

Problem

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

Implement the MinStack class:

  • MinStack() initializes the stack object.
  • void push(int val) pushes the element val onto the stack.
  • void pop() removes the element on the top of the stack.
  • int top() gets the top element of the stack.
  • int getMin() retrieves the minimum element in the stack.

You must implement a solution with O(1) time complexity for each function.

Example 1:

Input
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]

Output
[null,null,null,null,-3,null,0,-2]

Explanation
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); // return -3
minStack.pop();
minStack.top();    // return 0
minStack.getMin(); // return -2

Constraints:

  • -231 <= val <= 231 - 1
  • Methods pop, top and getMin operations will always be called on non-empty stacks.
  • At most 3 * 104 calls will be made to push, pop, top, and getMin.

Solution

I know that we can implement a stack very easily with just a linked list. The problem is keeping track of the minimum element. My mind jumps immediately to a priority queue/min heap, but that won’t meet the O(1) runtime requirement of the problem.

I think we can solve this by keeping a second stack with the minimum seen value. This works since stacks are LIFO and don’t allow arbitrary inserts/removals.

class MinStack {

    LinkedList<Integer> l;
    LinkedList<Integer> min;

    public MinStack() {
        l = new LinkedList<>();
        min = new LinkedList<>();
    }

    public void push(int val) {
        Optional<Integer> currentMin = Optional.empty();
        if (min.size() > 0) {
            currentMin = Optional.of(getMin());
        }

        l.push(val);

        if (currentMin.isPresent()) {
            min.push(Math.min(currentMin.get(), val));
        } else {
            min.push(val);
        }
    }

    public void pop() {
        l.pop();
        min.pop();
    }

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

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

Recent posts from blogs that I like

Prompts.js

via Simon Willison

Paintings of the Coast of California 1

From Albert Bierstadt's visit to the Farallon Islands in 1872, to George Bellows in 1917, with paintings from Mannheim, Granville Redmond and others in between.

via The Eclectic Light Company

How I write essays

Notes on process

via Henrik Karlsson