Correct min stack (?)
This commit is contained in:
parent
a3819c9b26
commit
3f369e1e70
@ -143,22 +143,18 @@ class Roman:
|
|||||||
class MinStack:
|
class MinStack:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._v: list[int] = []
|
self._v: list[int] = []
|
||||||
self._min: int | None = None
|
self._min: list[int] = []
|
||||||
|
|
||||||
def push(self, val: int) -> None:
|
def push(self, val: int) -> None:
|
||||||
self._v.append(val)
|
self._v.append(val)
|
||||||
|
self._min.append(min(val, self._min[-1] if self._min else val))
|
||||||
if self._min is None or self._min > val:
|
|
||||||
self._min = val
|
|
||||||
|
|
||||||
def pop(self) -> None:
|
def pop(self) -> None:
|
||||||
popped = self._v.pop(-1)
|
self._v.pop(-1)
|
||||||
|
self._min.pop(-1)
|
||||||
if popped == self._min:
|
|
||||||
self._min = min(self._v)
|
|
||||||
|
|
||||||
def top(self) -> int:
|
def top(self) -> int:
|
||||||
return self._v[-1]
|
return self._v[-1]
|
||||||
|
|
||||||
def getMin(self) -> int: # no qa
|
def getMin(self) -> int: # no qa
|
||||||
return typing.cast(int, self._min)
|
return self._min[-1]
|
||||||
|
Loading…
Reference in New Issue
Block a user