mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Create stack_II.py
This commit is contained in:
parent
e0d4dd2066
commit
1ddc83c1ff
38
stacks/stack_II.py
Normal file
38
stacks/stack_II.py
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
#!/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 != 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
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
def top(self) -> int:
|
||||||
|
if self.stack:
|
||||||
|
return self.stack[-1][0]
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
def getMin(self) -> int:
|
||||||
|
if self.stack:
|
||||||
|
return self.min
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user