Wins the min function offer Q20 stack comprising

Description Title
data structure definition stack, implement this type can be a min function smallest elements contained in the stack (should the time complexity O (1)).

Did not start understanding the problem, read the solution to a problem they would understand, this question is to make us realize this min with a stack data structure () function

See cattle cool off from the Internet to provide an idea of ​​the end of the day feeling good:

Link: https: //www.nowcoder.com/questionTerminal/4c776177d2c04c2494f2555c9fcc1e49 answerType = 1 & f = discussion?
Source: Cattle-off network

Method a: Method simple dual stack
when returning min value stack, and if only one auxiliary variable min, it may be because the value of the element to be popped min and failure, it is conventional practice to add an additional synchronization stack (Stack min ) for all values of min before saving the record, it is the equivalent of using auxiliary variables n, the space complexity is O (n).

Method Two: Compression reduction
we find that in fact the minimum value min itself is a kind of redundant information . why? Since each element included in the numerical value, the min value , for example, assume that the stack sequence is: 4,5,6,3,2,1, then the min value corresponding to each round is: 4,4,4 , 3,2,1, found:
4 = 4 + 4 + 0,5 = 1,6 = 4 + 4 + 3 = (- 1), 3 + 2 = (- 1), + 1 = 2 (-1); each element on the value already contains the minimum value before it;
so, we are not stored as long as 0,1,2, -1, -1, -1 in the data stack, and then using an auxiliary variable min = 1 can it?
Thus, according to the value of a single auxiliary variables stored in the stack and it is possible to infer the top value and the min value , the following rules:

Stack:
Compression: the element to be pushed on subtracting the current minimum value min, to obtain a difference diff, only the difference is stored;
update: If the push element the current value is smaller than the minimum value min, will have a minimum value update : min = value;
initial: first Drawing special, because the variable is not the min value, so that: min = value;

Stack:
Update: if the difference is stored in the stack diff is negative, indicating that the stack of the current element is the minimum value min, the min value is updated as needed on a minimum value min = min - diff, otherwise, the stack element is not minimum value, the variable is not to do anything min;
reduction: if the difference is stored in the stack diff is positive, indicating top = min + diff, otherwise, there is a minimum element itself top = top min;

A method of implementation code:

Auxiliary stack while the stack of data storage and minimum apart, top, and can be distinguished min

class Solution:
    def __init__(self):
        self.copy = []
    def push(self, node):
        # write code here
        copyMin = self.min()
        if copyMin == None or node < copyMin:
            copyMin = node
        self.copy.append((node,copyMin))
    def pop(self):
        # write code here
        if len(self.copy)==0:
            return None
        else:
            return self.copy.pop()
    def top(self):
        # write code here
        if len(self.copy)==0:
            return None
        else:
            return self.copy[len(self.copy)-1][0]
    def min(self):
        # write code here
        if len(self.copy) ==0:
            return None
        else:
            return self.copy[len(self.copy)-1][1]
Published 15 original articles · won praise 0 · Views 105

Guess you like

Origin blog.csdn.net/kikiwindsky/article/details/104365765