Niuke.com's sword refers to Offer - a stack containing the min function

Topic description

To define the data structure of the stack, please implement a min function in this type that can get the smallest element of the stack. The time complexity of calling min, push and pop is O(1).

method one

Consider using two stacks, a data stack (that is, a normal stack ), to implement push, pop and other operations of the stack; another auxiliary stack is used to store the smallest element in the stack for each stack operation (including push and pop).

Operation of the auxiliary stack: the top element of the stack is the smallest element in the current stack

  • To get the smallest element in the current stack, just return the top element of the stack;
  • Each time a push operation is performed, the size relationship between the pushed element and the top element of the stack is checked. Push the smaller element to the auxiliary stack.
  • When the pop operation is performed, the top element in the auxiliary stack is popped out.

Example:


code

class Solution {
public:
    stack<int> data;
    stack <int> minNum;
     
    void push(int value) {
        data.push(value);
        if( minNum.empty() )
            minNum.push(value);
        else
            minNum.push (minNum.top () <value? minNum.top (): value);
    }
    void pop() {
//        assert( !data.empty() && !minNum.empty() );
        data.pop();
        minNum.pop ();
    }
    int top() {
        return data.top();
    }
    int min() {
        return minNum.top();
    }
};
Simple optimization: For the operation of the auxiliary stack, every time a push operation is performed, check whether the pushed element is less than or equal to the top element of the auxiliary stack. If it is, push the element to the auxiliary stack as well. When performing a pop operation, check whether the popped element is equal to the current minimum value. If it is the same, you need to pop the element from the auxiliary stack. In other cases, the auxiliary stack can be left unchanged.

Method Two

The solution uses the stored difference without the need for an auxiliary stack, and the method is more clever. This method requires an additional space to store the minimum value in the current stack, and the minimum value is always stored at the top of the stack.
  • When a new value value needs to be pushed, the minimum value is first popped out, the difference between value and the minimum element in the current stack is pushed into the stack, and then the value is compared with the size of the minimum element in the current stack, and the smaller one between them is compared. The value is pushed onto the stack.
  • When executing the min() function, you can directly return to the top element of the stack.
  • When the top() function is executed, the sum of the two elements at the top of the stack can be returned.
  • When the pop() function is executed, the two values ​​at the top of the stack are first popped, which are the minimum value min in the current stack and the difference between the last pushed element and the minimum value in the stack, because the pop() function The last stored value needs to be deleted, so just consider the minimum value min after pop and push it onto the stack. If diff<0, it means that the last element pushed onto the stack is the smallest element, so just push min-diff onto the stack, and min-diff is the minimum value of the remaining elements in the stack after the current element is popped. And if diff>=0 and the stack is not empty, it means that the current value is not the minimum value, so the minimum value min needs to be pushed into the stack.
一个实例如下:
clear(): [ ] 
push(3): [3 3] 
push(4): [3 1 3] 
push(2): [3 1 -1 2] 
push(5): [3 1 -1 3 2] 
push(1): [3 1 -1 3 -1 1] 
push(1): [3 1 -1 3 -1 0 1] 
push(6): [3 1 -1 3 -1 0 5 1] 
push(7): [3 1 -1 3 -1 0 5 6 1]

min() --> 1; pop() --> 7: [3 1 -1 3 -1 0 5 1] 
min() --> 1; pop() --> 6: [3 1 -1 3 -1 0 1] 
min() --> 1; pop() --> 1: [3 1 -1 3 -1 1] 
min() --> 1; pop() --> 1: [3 1 -1 3 2] 
min() --> 2; pop() --> 5: [3 1 -1 2] 
min() --> 2; pop() --> 2: [3 1 3] 
min() --> 3; pop() --> 4: [3 3] 
min() --> 4; pop() --> 3: [ ]

code

class Solution {
public:
    stack<int> data;
    void push(int value) {
        if( data.empty() )
        {
            data.push(value);
            data.push(value);
            return;
        }
        int min = data.top();
        data.pop();
        if( min <= value )
        {
            data.push(value-min);
            data.push(min);
            return;
        }
        else
        {
            data.push(value-min);
            data.push(value);
            return;
        }
        return;
    }
    void pop() {
        int min = data.top();
        data.pop();
        if( data.top() >= 0 )
        {
            data.pop();
            data.push(min);
            return;
        }
        else
        {
            int temp = data.top();
            data.pop();
            data.push(min-temp);
        }
        return;
    }
    int top() {
        int min = data.top();
        data.pop();
        int res = data.top()+min;
        data.push(min);
        return res;
    }
    int min() {
        return data.top();
    }
};

参考文献:

https://www.cnblogs.com/javawebsoa/archive/2013/05/21/3091727.html

https://blog.csdn.net/sgbfblog/article/details/7752878

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325685142&siteId=291194637