Last active
April 10, 2020 07:40
-
-
Save rejoycesong/4e1e05d3e2f3c484bc0742a23218de5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 56ms | |
class MinStack: | |
def __init__(self): | |
self.minHistory = [float('inf')] | |
self.stack = [] | |
def push(self, x: int) -> None: | |
self.stack += [x] | |
if x <= self.minHistory[-1]: | |
self.minHistory.append(x) | |
def pop(self) -> None: | |
if self.stack: | |
if self.stack[-1] == self.minHistory[-1]: | |
self.minHistory.pop() | |
self.stack.pop() | |
def top(self) -> int: | |
return self.stack[-1] | |
def getMin(self) -> int: | |
return self.minHistory[-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment