Skip to content

Instantly share code, notes, and snippets.

@rejoycesong
Last active April 10, 2020 07:40
Show Gist options
  • Save rejoycesong/4e1e05d3e2f3c484bc0742a23218de5e to your computer and use it in GitHub Desktop.
Save rejoycesong/4e1e05d3e2f3c484bc0742a23218de5e to your computer and use it in GitHub Desktop.
# 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