Update and rename stack_II.py to stack.py

This commit is contained in:
marina 2023-08-07 17:58:03 -07:00 committed by GitHub
parent 27bc931a53
commit 6ed5cb896b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

31
stacks/stack.py Normal file
View file

@ -0,0 +1,31 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
class Stack:
def __init__(self):
self.stack = []
self.min = None
def push(self, val: int) -> None:
self.stack.append((val, self.min))
if self.min is not None:
self.min = min(self.min, val)
else:
self.min = val
def pop(self) -> None:
if self.stack:
(val, prior_min) = self.stack.pop()
self.min = prior_min
return val
def top(self) -> int:
if self.stack:
return self.stack[-1][0]
def get_min(self) -> int:
return self.min