forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.py
More file actions
46 lines (31 loc) · 1.16 KB
/
Stack.py
File metadata and controls
46 lines (31 loc) · 1.16 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
40
41
42
43
44
45
46
# Time Complexity: O(1)
# Space Complexity: O(2n)
# Did this code successfully run on LeetCode: Yes
# Any problem you faced while coding this: I had some issues with the syntax; other than that, I am good
# Your code here along with comments explaining your approach:
#initialized two stacks one for tracking the actual value and one for minimum value
#updated the min in in push and pop because whenever a number comes or goes the min changes
class MinStack:
def __init__(self):
self.st = []
self.minSt = []
self.minimum = float("inf")
self.minSt.append(self.minimum)
def push(self, val: int) -> None:
self.st.append(val)
self.minimum = min(self.minimum,val)
self.minSt.append(self.minimum)
def pop(self) -> None:
self.st.pop()
self.minSt.pop()
self.minimum = self.minSt[-1]
def top(self) -> int:
return self.st[-1]
def getMin(self) -> int:
return self.minimum
# 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()