Add solution for arrays: Mininum stack

This commit is contained in:
pro100ton 2025-02-17 20:56:52 +03:00
parent 5ad7e901cd
commit bd2d55fafe
3 changed files with 35 additions and 0 deletions

View file

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 KiB

View file

@ -0,0 +1,35 @@
class MinStack:
def __init__(self):
self.stack = []
self.min_stack = []
def push(self, val: int) -> None:
self.stack.append(val)
if not self.min_stack:
self.min_stack.append(val)
else:
self.min_stack.append(min(val, self.min_stack[-1]))
def pop(self) -> None:
if self.min_stack:
self.min_stack.pop()
if self.stack:
self.stack.pop()
else:
return None
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
if self.min_stack:
return self.min_stack[-1]
else:
return None
if __name__ == "__main__":
s = MinStack()
s.push(12)
s.pop()
s.pop()
print(s.getMin())