Skip to content

Instantly share code, notes, and snippets.

@munguial
Created April 11, 2020 01:59
Show Gist options
  • Select an option

  • Save munguial/4dcbbd8c96c5385a1d3f80580f5156fe to your computer and use it in GitHub Desktop.

Select an option

Save munguial/4dcbbd8c96c5385a1d3f80580f5156fe to your computer and use it in GitHub Desktop.
Day 10 - Min Stack
# 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