LeetCode – Min Stack (Java)

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

push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
getMin() -- Retrieve the minimum element in the stack.

Java Solution

To make constant time of getMin(), we need to keep track of the minimum element for each element in the stack. Define an element class that holds element value, min value, and pointer to elements below it.

class Elem{
    public int value;
    public int min;
    public Elem next;
 
    public Elem(int value, int min){
        this.value = value;
        this.min = min;
    }
}
 
public class MinStack {
    public Elem top;
 
    /** initialize your data structure here. */
    public MinStack() {
 
    }
 
    public void push(int x) {
        if(top == null){
            top = new Elem(x, x);
        }else{
            Elem e = new Elem(x, Math.min(x,top.min));
            e.next = top;
            top = e;
        }
 
    }
 
    public void pop() {
        if(top == null)
            return;
        Elem temp = top.next;
        top.next = null;
        top = temp;
 
    }
 
    public int top() {
        if(top == null)
            return -1;
        return top.value;
    }
 
    public int getMin() {
        if(top == null)
            return -1;
        return top.min;
    }
}
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • mukthar ahmed

    I felt the code is all good, delivering to what is required. At a given point of time,/node leve, we need the min-value in the stack and every node carrying a property (Node.min) is satisfied O(1)

  • Here is my code. I guess it works fine !

    class MinStack {
    Node head;

    public void push(int x) {
    if(head == null) {
    head = new Node(x);
    head.min = x;
    }
    else {
    Node temp = new Node(x);
    if(temp.min > head.min) temp.min = head.min;
    temp.next = head;
    head = temp;
    }
    }

    public void pop() {
    if(head == null ) return ;
    head = head.next;
    }

    public int top() {
    if(head == null ) return Integer.MAX_VALUE;
    return head.value;
    }

    public int getMin() {
    if(head == null ) return Integer.MAX_VALUE;
    return head.min;
    }
    }

    class Node {
    int value;
    int min;
    Node next;
    Node(int x) { value = x; next = null; min = x;}
    }

  • Tank

    Actually, this works:

    class MinStack {

    int[] arr = new int [100];

    int index = -1;

    int[] min = new int [100];

    public void push(int x) {

    if (index == arr.length – 1) {

    arr = Arrays.copyOf(arr, 2*arr.length);

    min = Arrays.copyOf(min, 2*min.length);

    }

    index++;

    arr[index] = x;

    min[index] = index == 0 ? x : Math.min(x, min[index-1]);

    }

    public void pop() {

    if(index == arr.length/2 – 1 && arr.length > 100) {

    arr = Arrays.copyOf(arr, arr.length/2);

    min = Arrays.copyOf(min, min.length/2);

    }

    index–;

    }

    public int top() {

    if (index > -1) {

    return arr[index];

    }

    else {

    return -1;

    }

    }

    public int getMin() {

    if(index > -1) {

    return min[index];

    }

    else {

    return -1;

    }

    }

    }

  • Tank

    I guess you could add it to a HashMap, along with the number of times that min value appears.. so if the min value gets popped, you either remove it from the hash map if its value is 1, or decrement the # of times it appears

  • MyCat

    Just keep the min value is not enough, if your next pop removes the min value, then you need know the new min value, which still needs O(n) time to search

  • Tank

    you’re right. You should have a minimum value declared in this class, and every time you push a new number to the stack, check if it is a lower value than the current min, and update that way, so when you call getMin it’s 0(1)

  • Justin Stein

    Am I having a brain fart? How is this retrieving the minimum value in constant runtime if it iterates through the entire stack? That is O(n) runtime.