-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathMinStack.py
More file actions
39 lines (30 loc) · 1.04 KB
/
MinStack.py
File metadata and controls
39 lines (30 loc) · 1.04 KB
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
39
# Time Complexity : O(1) for all operations
# Space Complexity : O(n) for all operations
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Your code here along with comments explaining your approach
## Using 2 lists with one storing the actual numbers and the 2nd storing the minimum value at every instance
class MinStack:
def __init__(self):
self.Stack = []
self.minStack = []
self.min = float('inf')
self.minStack.append(self.min)
def push(self, val: int) -> None:
self.min = min(val, self.min)
self.Stack.append(val)
self.minStack.append(self.min)
def pop(self) -> None:
self.Stack.pop()
self.minStack.pop()
self.min = self.minStack[-1]
def top(self) -> int:
return self.Stack[-1]
def getMin(self) -> int:
return self.minStack[-1]
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()