We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent be31e0c commit 729dc79Copy full SHA for 729dc79
Python/min-stack.py
@@ -68,3 +68,26 @@ def top(self):
68
def getMin(self):
69
return self.minStack[-1][0]
70
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