Skip to content

Latest commit

 

History

History
56 lines (43 loc) · 976 Bytes

155.md

File metadata and controls

56 lines (43 loc) · 976 Bytes

155. Min Stack

Solution 1 (time O(1), space(O(n))

class MinStack(object):

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.stack = []
        self.min_stack = []


    def push(self, x):
        """
        :type x: int
        :rtype: None
        """
        self.stack.append(x)
        if self.min_stack:
            self.min_stack.append(min(self.min_stack[-1], x))
        else:
            self.min_stack.append(x)


    def pop(self):
        """
        :rtype: None
        """
        self.stack.pop()
        self.min_stack.pop()


    def top(self):
        """
        :rtype: int
        """
        return self.stack[-1]


    def getMin(self):
        """
        :rtype: int
        """
        return self.min_stack[-1]


# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()