Open SleeplessChallenger opened 2 years ago
I would like to modify @SleeplessChallenger's solution a bit more. Here is my version
class MinStack(object):
def __init__(self):
self.stack = list()
def push(self, val):
minVal = min(val, self.getMin()) if self.stack else val
self.stack.append((val, minVal))
def pop(self):
self.stack.pop()
def top(self):
return self.stack[-1][0]
def getMin(self):
return self.stack[-1][1]
Hey, I created a solution that has O(n) space instead of O(2n) with 2 stacks: