This topic is very classic in Haitao's book. Let me do it directly.
My idea is to use an auxiliary stack to push an element into the stack when it is smaller than the minimum element of the current auxiliary stack, and record the number of elements in the stack when every minimum value is pushed into the stack.
For example, the stack sequence is 5, 7, 8, 2, and 1.
The secondary stack is in the form of 5 (1) 3 (1) 2 (1) 1 (1). Therefore, I am using a secondary stack and adding an auxiliary variable, therefore, when the original pop Stack was used, I reduced the number of auxiliary stack variables by 1.
5 (2) 3 (2)
If the value is 0, the stack is output.
After reading the ideas of Haitao, I am suddenly enlightened. The Ox is the ox.
The idea is that if the element inserted to the original stack is greater than the minimum element of the current secondary stack, the secondary stack will still push the current minimum element, which is too cool.
Original stack 5 7 3 8 2 1
Auxiliary stack 5 5 3 3 2 1