Couldn’t you simply keep track of the smallest value while pushing items into the stack? This is for the problem “Challenge 9: min( ) Function Using a Stack”.
class MinStack:
def __init__(self):
self.min_value = None
self.stack = []
def pop(self):
self.stack.pop()
def push(self, value):
if not self.min_value or value < self.min_value:
self.min_value = value
self.stack.append(value)
def min(self):
return self.min_value