#leetCode brush title documentary Day8

https://leetcode-cn.com/problems/min-stack/

A support designed to push, pop, top operation, the stack can be retrieved and smallest elements in constant time.

push (x) - the element x push the stack.
pop () - delete elements of the stack.
top () - get the top element.
getMin () - retrieve the minimum element in the stack.


Example:

Mins Thanks mins thanks = new Mins Thanks ();
minStack.push (-2);
minStack.push (0);
minStack.push (-3);
minStack.getMin (); ->返回-3.
minStack.pop ();
minStack.top (); ->返回0.
minStack.getMin (); ->返回-2.

 

Chicken dishes to try:

I can understand what is not very constant time (guess O (1)?) So the mentality of holding the time-out will start to write the most common approach.

 

 1 class MinStack {
 2 public:
 3     /** initialize your data structure here. */
 4     stack<int> main;
 5     stack<int> b;
 6     MinStack() {
 7         
 8     }
 9     
10     void push(int x) {
11         main.push(x);
12     }
13     
14     void pop() {
15         main.pop();
16     }
17     
18     int top() {
19         return main.top();
20     }
21     
22     int getMin() {
23         int result = INT_MAX;
24         while (!main.empty()) {
25             if (main.top() < result) result = main.top();
26             b.push(main.top());
27             main.pop();
28         }
29         while (!b.empty()) {
30             main.push(b.top());
31             b.pop();
32         }
33         return result;
34     }
35 };
36 
37 /**
38  * Your MinStack object will be instantiated and called as such:
39  * MinStack* obj = new MinStack();
40  * obj->push(x);
41  * obj->pop();
42  * int param_3 = obj->top();
43  * int param_4 = obj->getMin();
44  */

 

The result was a timeout, big brother is to see how to write it

 

Worship Gangster Code:

Borrowing auxiliary stack space for time! !

 1 class MinStack {
 2 public:
 3     /** initialize your data structure here. */
 4     stack<int> main;
 5     stack<int> b;
 6     MinStack() {
 7         
 8     }
 9     
10     void push(int x) {
11         main.push(x);
12         if (b.empty() || b.top() >= x) b.push(x);
13     }
14     
15     void pop() {
16         int top = main.top();
17         main.pop();
18         if (top == b.top()) {
19             b.pop();
20         }
21     }
22     
23     int top() {
24         return main.top();
25     }
26     
27     int getMin() {
28        return b.top();
29     }
30 };
31 
32 /**
33  * Your MinStack object will be instantiated and called as such:
34  * MinStack* obj = new MinStack();
35  * obj->push(x);
36  * obj->pop();
37  * int param_3 = obj->top();
38  * int param_4 = obj->getMin();
39  */

B stack effect is the smallest element is stored

When the element is pushed onto the stack to be less than or equal b or b stack top element stack is empty, the data in the main stack when the stack while the stack is also b stack (why is not equal to greater than greater than it, see example :)

 

Similarly, when the operation of the stack, if the stack is just an element b stack top element, while the top of the stack is also popped b.

 

 

 

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/min-stack
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/xyy999/p/11801884.html