Skip to content

Instantly share code, notes, and snippets.

@RP-3
Created April 11, 2020 23:34
Show Gist options
  • Save RP-3/402b12d6ee0393a4fdf55dad8041d4b9 to your computer and use it in GitHub Desktop.
Save RP-3/402b12d6ee0393a4fdf55dad8041d4b9 to your computer and use it in GitHub Desktop.
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.mins = []
self.stack = []
def push(self, x: int) -> None:
self.stack.append(x)
if not len(self.mins) or x <= self.mins[-1]:
self.mins.append(x)
def pop(self) -> None:
if not len(self.stack): return None
last = self.stack.pop()
if last == self.mins[-1]: self.mins.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.mins[-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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment