Skip to content

155. Min Stack

https://leetcode.com/problems/min-stack/

py
class MinStack(object):

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

    def push(self, x):
        """
        :type x: int
        :rtype: void
        """
        if len(self.data) == 0:
            self.data.append([x, x])
        else:
            self.data.append([x, min(self.data[-1][1], x)])

    def pop(self):
        """
        :rtype: void
        """
        a = self.data.pop()
        return a[0]

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

    def getMin(self):
        """
        :rtype: int
        """
        return self.data[-1][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()