-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMinStack.py
More file actions
35 lines (29 loc) · 844 Bytes
/
Copy pathMinStack.py
File metadata and controls
35 lines (29 loc) · 844 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
28
29
30
31
32
33
34
35
import math
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
self.min = math.inf
def push(self, x: int) -> None:
self.x = x
self.stack.append(x)
if(x < self.min):
self.min = x
def pop(self) -> None:
t = self.stack.pop()
if(t==self.min and len(self.stack)):
self.min = min(self.stack)
elif(t==self.min and not len(self.stack)):
self.min = math.inf
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.min
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()