Jianzhi Offer 30 contains the stack of the min function

It is still a stack. Since I did 09 first, when looking at this question, I gave priority to using dual stacks. Similarly, the first stack is a data stack, and the second stack is an auxiliary stack (the smallest stack).

In the comment solution of this question, the animation and vernacular solution given by the boss are more popular and detailed. Here I write something simpler and I can understand it.

First understand the role of the data stack and the auxiliary stack (minimum stack). As the name suggests, the data stack means that all elements are pushed into the data stack, and the minimum stack is the minimum value of the elements in the data stack after each input element is pushed into the data stack. as the picture shows

Initially, push element 2 onto the data stack

After pushing, since there is only one element 2 in the data stack, the minimum element in the minimum stack at this time is 2

Push an element 3 again, at this time there are two elements 2 and 3 in the data stack

Then the minimum stack is pushed into the minimum value of the elements in the data stack at this time. At this time, there are only two elements 2 and 3 in the data stack, and the minimum value is 2, then continue to push the element 2 into the minimum stack.

If you continue to push element 1 into the data stack, then the data stack elements are: 2, 3, 1

Then the minimum value of the three elements in the data stack becomes 1 at this time, so the data pushed into the minimum stack is 1

Through such steps, we can find that the top element of the smallest stack is always the smallest element in the data stack 

In this way, we can write the push method by judging

And pop means that all elements of the dual stack are popped

top returns the top element of the data stack

According to the above red summary, min returns the top element of the smallest stack

Write code according to ideas

class MinStack {

    Stack<Integer> data_stack;
    Stack<Integer> min_stack;

    /** initialize your data structure here. */
    public MinStack() {
        data_stack = new Stack<>();
        min_stack = new Stack<>();
    }
    
    public void push(int x) {
        if(min_stack.isEmpty()){ //最小栈为空,说明数据栈也为空,则把元素压入最小栈
            min_stack.push(x);
        }else{ //最小栈不为空,说明数据栈也不为空,而最小栈栈顶元素为数据栈的最小元素,则进行判断比较
            if(min_stack.peek() > x){ //最小栈的栈顶元素 > x ,说明 x 更小,应该压入最小栈
                min_stack.push(x);
            }else{
                //最小栈的栈顶元素 < x ,说明 x 更大,则最小栈压入上次记录的最小元素
                min_stack.push(min_stack.peek());
            } 
        }
        data_stack.push(x);  
    }
    
    public void pop() {
        data_stack.pop();
        min_stack.pop();
    }
    
    public int top() {
        return data_stack.peek();
    }
    
    public int min() {
        return min_stack.peek();
    }
}

Here you need to distinguish between pop() and peek()

pop() returns the top element of the stack and removes it in the process.

peek() returns the element at the top of the stack without removing it from the stack.

isEmpty() is to judge whether it is empty, if the stack is empty, it returns true, if there are elements in the stack, it returns false

Guess you like

Origin blog.csdn.net/Sean_Asu/article/details/122890280