[LeetCode][python3]Day10. Min Stack (30-Day LeetCoding Challenge)

class MinStack:
def __init__(self):
self.s=[]

def push(self, x: int) -> None:
self.s.append(x)

def pop(self) -> None:
del self.s[-1]

def top(self) -> int:
return self.s[-1]

def getMin(self) -> int:
return min(self.s)
class MinStack:
def __init__(self):
self.stack = []
self.min = []

def push(self, x: int) -> None:
self.stack.append(x)
if len(self.min) == 0:
self.min.append(x)
elif self.min[-1] >= x:
self.min.append(x)

def pop(self) -> None:
val = self.stack.pop()
if val == self.min[-1]:
self.min.pop()

def top(self) -> int:
return self.stack[-1]

def getMin(self) -> int:
return self.min[-1]

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store