Created
April 11, 2020 01:59
-
-
Save munguial/4dcbbd8c96c5385a1d3f80580f5156fe to your computer and use it in GitHub Desktop.
Day 10 - Min Stack
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
| # https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/529/week-2/3292/ | |
| class MinStack: | |
| def __init__(self): | |
| self.stack = [] | |
| self.minStack = [] | |
| def push(self, x: int) -> None: | |
| self.stack.append(x) | |
| if len(self.minStack) == 0 or x <= self.minStack[-1]: | |
| self.minStack.append(x) | |
| def pop(self) -> None: | |
| if len(self.stack) > 0: | |
| x = self.stack.pop() | |
| if x == self.minStack[-1]: | |
| self.minStack.pop() | |
| def top(self) -> int: | |
| return self.stack[-1] | |
| def getMin(self) -> int: | |
| return self.minStack[-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment