Ideas:
The push and pop operations are completed within the constant time complexity. However, the min operation must be completed within the constant time complexity. At first, it is easy to think of saving a variable min in the stack, to save the minimum value, you only need to check the value of the min variable if you need the min () operation. this idea looks good, but there is a fatal defect, that is, to save only one min variable, so when min also needs to play the stack, the min in the stack also needs to be updated with the second smallest value. However, we cannot know which is the second smallest value. OK. We can also try to save the second smallest variable secmin, when the minimum value is on the stack, we use secmin to update the min variable. However, secmin also requires thirdmin update. If thirdmin is on the stack ....... in this case, we need to save Min, secmin, thirdmin ...... and so on. therefore, the method of saving constant variables obviously cannot meet the requirement. we can try to use a Data Structure to save the minimum values of this series.
Now the problem becomes. We need an extra space to save the current minimum value. Even if the current minimum value is out of the stack, we can still get the latest minimum value (regardless of the stack, how does the stack pressure process change ),
We can try to solve the problem by using the auxiliary Stack:
Store a data stack and a secondary stack in the newly defined stack. The secondary stack top stores the current minimum Min.
Min () operation: you only need to check the top elements of the secondary stack.
Push (_ Val): First press _ Val into the data stack, then compare _ Val with the top element of the secondary stack. If _ Val <top, press _ Val into the auxiliary stack to make _ Val the current minimum value.
Pop () operation: First play the top element of the data stack to the stack. If the top element of the stack is the minimum value, play the top element of the auxiliary stack (the minimum value is min)
The Code is as follows:
#include<iostream>#include<stack>using namespace std;struct MinStack{private:stack<int> St;stack<int> Extern;public:void push(int val){St.push(val);if(Extern.empty() || val <= Extern.top())Extern.push(val);}int pop(){if(St.empty()){cout<<"Hello"<<endl;throw "Error";}int Temp=St.top();St.pop();if(Temp == Extern.top())Extern.pop();return Temp;}int min(){if(Extern.empty()){cout<<"Hello2"<<endl;throw "error!";}return Extern.top();}};void main(){MinStack ms;ms.push(2);ms.push(2);ms.push(3);ms.pop();ms.pop();ms.pop();cout<<ms.min();}
There is also a clever solution:
Because the min () operation needs to be completed within the complexity of theta (1), we can try to place min on the top of the stack. However, the push () operation and POP () Operation () the Operation also needs to be completed within theta (1) time complexity. What should I do? First, let's consider POP (). If the top element of the stack is the smallest element, you only need to bring Min to the stack. What if the top element of the stack is larger than min? We can save element-min in the next element of Min. Two elements can pop up in the stack, and then calculate the sum of the two elements. (think: is the element smaller Than Min? Impossible, because Min is the minimum value)
When pushing (_ Val), because the stack operation here comes with two elements, when the stack is empty, _ Val is pushed twice into the stack, and the top of the stack is Min. when the stack is not empty, replace min with (_ Val-min) and min {_ Val, min}
The specific operation is: min out stack, push (_ Val-min), push (_ Val <min? _ Val: min)
Code:
# Include <iostream> # include <stack> using namespace STD; struct minstack {PRIVATE: Stack <int> st; public: void push (INT Val) {If (St. empty () {St. push (VAL); ST. push (VAL);} else {int min = ST. top (); ST. pop (); ST. push (Val-min); ST. push (Val <min? VAL: min) ;}} int POP () {If (St. Empty () {cout <"Pop error! "<Endl; exit (1);} int min = ST. top (); ST. pop (); int flag = ST. top (); ST. pop (); // two if (flag <0) when the stack is popped up, and the flag <0 indicates that the pop-up value is the minimum value. You need to use the second smallest value to update the top of the stack {St. push (min-flag); Return min;} else {// If the pop-up value is not the minimum value, the stack pops up directly without updating the minimum value if (! St. empty () {St. push (min); Return flag + min;} return min ;}} int min () {If (St. empty () {cout <"Search min error! "<Endl; exit (1);} int min = ST. top (); Return min ;}}; void main () {minstack MS; Ms. pop (); Ms. pop (); Ms. pop (); cout <Ms. min ();}