mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-22 16:31:15 -04:00
Update and rename stack_II.py to stack.py
This commit is contained in:
parent
27bc931a53
commit
6ed5cb896b
1 changed files with 1 additions and 8 deletions
31
stacks/stack.py
Normal file
31
stacks/stack.py
Normal 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
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue