A python code.


  • 1
    def __init__(self):
        self.minValue = sys.maxint
        self.stack = []
    
    def push(self, x):
        """
        :type x: int
        :rtype: void
        """
        if x < self.minValue:
            self.minValue = x
        self.stack.append(x)
    
    def pop(self):
        """
        :rtype: void
        """
        if self.stack:
            if self.top() == self.minValue:
                self.stack.pop()
                if self.stack:
                    self.minValue = min(self.stack)
                else:
                    self.minValue = sys.maxint
            else:
                self.stack.pop()
        
    
    def top(self):
        """
        :rtype: int
        """
        if self.stack:
            return self.stack[-1]
        else:
            return None
            
    def getMin(self):
        """
        :rtype: int
        """
        return self.minValue

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.