A poorly-made min stack

This commit is contained in:
2023-10-21 07:18:56 -04:00
parent 345464d0d4
commit a3819c9b26
2 changed files with 73 additions and 0 deletions
+24
View File
@@ -138,3 +138,27 @@ class Roman:
total += cls.ALL[c]
return total
class MinStack:
def __init__(self):
self._v: list[int] = []
self._min: int | None = None
def push(self, val: int) -> None:
self._v.append(val)
if self._min is None or self._min > val:
self._min = val
def pop(self) -> None:
popped = self._v.pop(-1)
if popped == self._min:
self._min = min(self._v)
def top(self) -> int:
return self._v[-1]
def getMin(self) -> int: # no qa
return typing.cast(int, self._min)