# 011 to implement a stack, in addition to the push and pop operations, the min function is also implemented to return the minimum value in the stack. the time complexity is O (1) (keep it up)

Source: Internet
Author: User
To implement a stack, in addition to push and pop operations, you must also implement the min function to return the minimum value in the stack.
The time complexity of the push, pop, and Min functions is O (1 ).

The most direct response to this question is to use a variable to save the minimum value of the current stack. Let's see if this is feasible?
If the stack is pushed all the time, there is no problem. If the inbound stack element is smaller than the current minimum value, the current minimum value is updated.
However, if the top element of the POP stack is the minimum value, how can we update the minimum value? Obviously, it is not easy.
Since we can't solve this problem with only one variable, we will add the variable. If each node saves the current value,
In addition, the minimum value of a node from the node to the bottom of the stack is saved. Then, no matter which node becomes the top node of the stack,
We can all achieve the minimum values of the remaining elements. The price is doubled. No code is written.

The following describes a method. We can use a stack to save the minimum value of the stack:
For example, there is a set of numbers to be written into the stack: 10 11 6 2 12
Stack S1: 10 11 6 2 12
Stack S2: 10 6 2
That is, when the value reaches the top 12 of the stack, the minimum value is 2. If the value of S1 stack is 12, it is compared with s2.top. If the value is greater than s2.top, S2.
For any operation, if s2.top is equal to s2.top, s2.pop (). For example, 2 = s2.top () When s2.top 2 of S1. Then s2.pop ();
This saves a lot of space.
However, the above method has the following problems:
For example, data
S1: 10 11 6 2 2 2 12
S2: 10 11 6 2
When S1 has 2 out of the stack, S2 has 2 out of the stack, so that we can go to the minimum value of S1 is 6, which is obviously wrong. The solution is

Add a counter for each element in S2. In this way, when two stacks are generated three times in a row, two of the S2 stacks are generated.

`#include <iostream>#include <stack>struct DataCount{int data;int count;DataCount(int vData = std::numeric_limits<int>::infinity()) : data(vData), count(0) {}};class Stack{public:Stack() {}~Stack() {}void push(int vData){m_Data.push(vData);if (m_MinData.empty()) {m_MinData.push(DataCount(vData));}else{if (vData < m_MinData.top().data){m_MinData.push(DataCount(vData));}else if (vData == m_MinData.top().data){++m_MinData.top().count;}}}void pop(){if (m_Data.empty()) {std::cout << "Empty stack\n";return;}int Data = m_Data.top();m_Data.pop();if (Data == m_MinData.top().data){if (--m_MinData.top().count == 0){m_MinData.pop();}}}int getMin(){if (m_MinData.empty()){std::cout << "Empty stack\n";return;}return m_MinData.top().data;}private:std::stack<int>        m_Data;std::stack<DataCount>  m_MinData;};`

011 to implement a stack, in addition to the push and pop operations, the min function is also implemented to return the minimum value in the stack. the time complexity is O (1) (keep it up)

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.