{"id":151,"date":"2017-09-22T19:45:38","date_gmt":"2017-09-22T11:45:38","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=151"},"modified":"2018-11-27T16:46:16","modified_gmt":"2018-11-27T08:46:16","slug":"155-min-stack","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=151","title":{"rendered":"155. Min Stack"},"content":{"rendered":"<p>Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.<\/p>\n<ul>\n<li>push(x) &#8212; Push element x onto stack.<\/li>\n<li>pop() &#8212; Removes the element on top of the stack.<\/li>\n<li>top() &#8212; Get the top element.<\/li>\n<li>getMin() &#8212; Retrieve the minimum element in the stack.<\/li>\n<\/ul>\n<p><b>Example:<\/b><\/p>\n<pre>MinStack minStack = new MinStack();\r\nminStack.push(-2);\r\nminStack.push(0);\r\nminStack.push(-3);\r\nminStack.getMin();   --&gt; Returns -3.\r\nminStack.pop();\r\nminStack.top();      --&gt; Returns 0.\r\nminStack.getMin();   --&gt; Returns -2.<\/pre>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass MinStack(object):\r\n    def __init__(self):\r\n        &quot;&quot;&quot;\r\n        initialize your data structure here.\r\n        &quot;&quot;&quot;\r\n        self.min_stack = []\r\n        self.min_num_stack = []\r\n\r\n    def push(self, x):\r\n        &quot;&quot;&quot;\r\n        :type x: int\r\n        :rtype: void\r\n        &quot;&quot;&quot;\r\n        if len(self.min_num_stack) == 0 or x &lt;= self.min_num_stack[-1]:\r\n            self.min_num_stack.append(x)\r\n        self.min_stack.insert(0, x)\r\n\r\n    def pop(self):\r\n        &quot;&quot;&quot;\r\n        :rtype: void\r\n        &quot;&quot;&quot;\r\n        if self.min_stack[0] == self.min_num_stack[-1]:\r\n            self.min_num_stack.pop()\r\n        del self.min_stack[0]\r\n\r\n    def top(self):\r\n        &quot;&quot;&quot;\r\n        :rtype: int\r\n        &quot;&quot;&quot;\r\n        return self.min_stack[0]\r\n\r\n    def getMin(self):\r\n        &quot;&quot;&quot;\r\n        :rtype: int\r\n        &quot;&quot;&quot;\r\n        return self.min_num_stack[-1]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Design a stack that supports push, pop, top, and retrie<a href=\"https:\/\/www.ccagml.com\/?p=151\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">155. Min Stack<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/151"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=151"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":152,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/151\/revisions\/152"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}