Skip to content

Commit 729dc79

Browse files
committed
add a solution
1 parent be31e0c commit 729dc79

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Python/min-stack.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,26 @@ def top(self):
6868
def getMin(self):
6969
return self.minStack[-1][0]
7070

71+
# time: O(1)
72+
# space: O(n)
73+
74+
class MinStack3(object):
75+
76+
def __init__(self):
77+
self.stack = []
78+
79+
def push(self, x):
80+
if self.stack:
81+
current_min = min(x, self.stack[-1][0])
82+
self.stack.append((current_min, x))
83+
else:
84+
self.stack.append((x, x))
85+
86+
def pop(self):
87+
return self.stack.pop()[1]
88+
89+
def top(self):
90+
return self.stack[-1][1]
91+
92+
def getMin(self):
93+
return self.stack[-1][0]

0 commit comments

Comments
 (0)