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>

  1. Justin Stein on 2015-3-19

    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.

  2. Tank on 2015-4-4

    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)

  3. MyCat on 2015-4-5

    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

  4. Tank on 2015-4-6

    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

  5. Tank on 2015-4-11

    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;

    }

    }

    }

  6. Taoufik Bdiri on 2015-4-17

    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;}
    }

  7. mukthar ahmed on 2016-3-6

    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)

  8. Gayathri Muralidharan on 2016-12-14

    public class MinStack {
    PriorityQueue pq;
    Stack st;
    /** initialize your data structure here. */
    public MinStack() {
    pq=new PriorityQueue();
    st=new Stack();
    }

    public void push(int x) {
    pq.offer(x);
    st.push(x);
    }

    public void pop() {
    Integer x = st.pop();
    pq.remove(x);
    }

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

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

  9. Justin Heyes-Jones on 2017-2-14

    Using a priority queue gives you a program that is correct but does not meet the constant time performance requirements. Whilst peek can be constant time both push and pop will be logarithmic.

  10. Justin Heyes-Jones on 2017-2-14

    All the operations in the above example are constant time. Where is the iteration you’re seeing? If you don’t follow how the min is tracked in the stack values try working through the algorithm on paper.

  11. Asad Arhaan on 2017-3-1

    Using min heap.

    class MinStack {
    public:
    stack st;
    priority_queue<int,vector,greater> pq;

    void push(int x){
    st.push(x);
    pq.push(x);
    }

    void pop(){
    if(st.top()==pq.top()){
    st.pop();
    pq.pop();
    }
    else{
    st.pop();
    }
    }

    int top(){
    return st.top();
    }

    int getMin(){
    return pq.top();
    }

    };

Leave a comment

*