Created
April 11, 2020 23:34
-
-
Save RP-3/402b12d6ee0393a4fdf55dad8041d4b9 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
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