From 3f369e1e70d79379357584f4c1c5568acfc69f44 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 21 Oct 2023 07:25:38 -0400 Subject: [PATCH] Correct min stack (?) --- leetcode/stuff.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/leetcode/stuff.py b/leetcode/stuff.py index 0525b95..508338c 100644 --- a/leetcode/stuff.py +++ b/leetcode/stuff.py @@ -143,22 +143,18 @@ class Roman: class MinStack: def __init__(self): self._v: list[int] = [] - self._min: int | None = None + self._min: list[int] = [] def push(self, val: int) -> None: self._v.append(val) - - if self._min is None or self._min > val: - self._min = val + self._min.append(min(val, self._min[-1] if self._min else val)) def pop(self) -> None: - popped = self._v.pop(-1) - - if popped == self._min: - self._min = min(self._v) + self._v.pop(-1) + self._min.pop(-1) def top(self) -> int: return self._v[-1] def getMin(self) -> int: # no qa - return typing.cast(int, self._min) + return self._min[-1]