forked from wuduhren/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin-stack.py
More file actions
23 lines (17 loc) · 666 Bytes
/
min-stack.py
File metadata and controls
23 lines (17 loc) · 666 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#"stack" is just a normal stack.
#"minStack" stores the min of the stack element in the same position.
#i.e. At the time stack[x] put in to the stack. minStack[x] is the min.
class MinStack:
def __init__(self):
self.stack = []
self.minStack = []
def push(self, val: int) -> None:
self.stack.append(val)
self.minStack.append(val if (not self.minStack or val<self.minStack[-1]) else self.minStack[-1])
def pop(self) -> None:
self.minStack.pop()
return self.stack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.minStack[-1]