-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path155-Min-Stack.py
More file actions
executable file
·37 lines (28 loc) · 1.19 KB
/
155-Min-Stack.py
File metadata and controls
executable file
·37 lines (28 loc) · 1.19 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
class MinStack:
def __init__(self):
# Tops of the start are at the end of the list
self.stack = [] # Normal stack
self.minStack = [] # Stack of minimal values
def push(self, val: int) -> None:
self.stack.append(val) # Add to the normal stack
# If this value is the current minimum or a new minimum
if self.minStack == [] or (self.minStack != [] and val <= self.minStack[-1]):
self.minStack.append(val) # Add it to the minimum stack
def pop(self) -> None:
# If the top of the stack is equal to the top of the minimum stack
if self.stack[-1] == self.minStack[-1]:
# Remove the top of the minimum stack as this value is being removed
self.minStack = self.minStack[:-1]
self.stack = self.stack[:-1] # Remove the top of the stack
def top(self) -> int:
# Get the top of the normal stack
return self.stack[-1]
def getMin(self) -> int:
# Get the top of the minimum stack
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()