-
Notifications
You must be signed in to change notification settings - Fork 0
/
155.py
38 lines (33 loc) · 920 Bytes
/
155.py
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
28
29
30
31
32
33
34
35
36
37
38
class MinStack:
def __init__(self):
self.stack=[]
self.minstack=[]
def push(self, val):
self.stack.append(val)
if len(self.minstack)==0:
self.minstack.append(val)
else:
if val > self.minstack[-1]:
pass
else:
# while len(self.minstack)>0 and self.minstack[-1]>val:
# self.minstack.pop()
self.minstack.append(val)
def pop(self):
ele= self.stack.pop()
if len(self.minstack)>0 and ele== self.minstack[-1]:
self.minstack.pop()
def top(self):
return self.stack[-1]
def getMin(self):
if len(self.minstack)==0:
return None
return self.minstack[-1]
minStack =MinStack()
minStack.push(-2)
minStack.push(0)
minStack.push(-3)
minStack.getMin()
minStack.pop()
minStack.top()
minStack.getMin()