-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinStack.py
More file actions
27 lines (22 loc) · 708 Bytes
/
MinStack.py
File metadata and controls
27 lines (22 loc) · 708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
### leetcode 155
class MinStack:
def __init__(self):
self.stack = []
self.minstack = []
def push(self, x):
self.stack.append(x)
if len(self.minstack) and x == self.minstack[-1][0]:
self.minstack[-1][1] += 1
elif len(self.minstack)== 0 or x < self.minstack[-1][0]:
self.minstack.append([x,1])
def pop(self):
if self.top() == self.getMin():
if self.minstack[-1][1] > 1:
self.minstack[-1][1] -= 1
else:
self.minstack.pop()
return self.stack.pop()
def top(self):
return self.stack[-1]
def getMin(self):
return self.minstack[-1][0]