Prove safety offer: Stack (java) comprising a min function

Title: stack data structure definition, implement this type can be a min function smallest elements stack. In this stack, call min, push and pop time complexity is O (1).

    See this issue, our first reaction might be pressed into a time when a new element into the stack, the stack of all the elements of the sort, so that the smallest element top of the stack, so you can get the minimum in O (1) time the elements. But this idea does not guarantee the final element can be pushed out of the first stack, so this is not a data structure stack up.

    Then, remembering that we add a member variable to store the smallest element in the stack. Every time a new element is pushed onto the stack when, if the element is smaller than the current smallest element, the smallest element is updated. After hearing this line of thinking interviewer will ask: If the current smallest element is popped off the stack, and how to get to the next smallest element of it?

    Here we find the analysis to only add a member variable to store the smallest element is not enough, that is when the smallest element pop the stack, we hope to get the next smallest element. Therefore, before pressing into the smallest element, the second smallest element we want to save them. Therefore, before pressing into the smallest element, the second smallest element we want to save them. So we can put each of them into the smallest elements are stored in another auxiliary stack.

    To analyze the elements of the process pushed or popped off the stack, we wish to give a few examples.


    We can see from Table 4.1, if every time the smallest element pressed into the auxiliary stack, so we can guarantee the auxiliary stack top of the stack is always the smallest element. When the pop-up from the minimum element in the data stack, while the auxiliary stack pop stack elements, in which case the auxiliary stack is the top element of the new next minimum.

class ListNode<K>{  
    K data;  
    ListNode<K> nextNode;  
}  
public class MyStack<K> {  
    /** 
     * 定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数 
     * 在该栈中,min、push、pop的时间复杂度都是O(1) 
     */  
    public ListNode<K> head;  
    public int length;  
    public void push(K item){  
        ListNode<K> node = new ListNode<K>();  
        node.data = item;  
        node.nextNode = head;  
        head = node;  
        length++;  
    }  
    public K pop(){  
        if(head == null)  
            return null;  
        ListNode<K> temp = head;  
        head = head.nextNode;  
        length--;  
        return temp.data;  
    }  
      
}  

private MyStack<Integer> minStack = new MyStack<>();  
private MyStack<Integer> dataStack = new MyStack<>();  
public void push(Integer item){  
        dataStack.push(item);  
        if(minStack.length == 0 || item<=minStack.head.data){  
            minStack.push(item);  
        }else{  
            minStack.push(minStack.head.data);  
        }  
    }  
    public Integer pop(){  
        if(dataStack.length == 0 || minStack.length == 0){  
            return null;  
        }  
        minStack.pop();  
        return dataStack.pop();  
    }  
    public Integer min(){  
        if(minStack.length == 0){  
            return null;  
        }  
        return minStack.head.data;  
    }  



Published 118 original articles · won praise 35 · views 120 000 +

Guess you like

Origin blog.csdn.net/abc7845129630/article/details/52728926