forked from lupamo3-zz/coding-interview-gym
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Min_Max_Stack_Construction.py
33 lines (28 loc) · 1.15 KB
/
Min_Max_Stack_Construction.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
class MinMaxStack:
def __init__(self):
# only contains the min and max value at any given time
# min_max_stack is an array of dictionary, which is the same length of stack
self.min_max_stack = [] # an array of dictionary of min and max key
self.stack = []
# O(1) time | O(1) space
def peek(self):
return self.stack[len(self.stack) - 1]
# O(1) time | O(1) space
def pop(self):
self.min_max_stack.pop()
return self.stack.pop()
# O(1) time | O(1) space
def push(self, number):
new_min_max = {"min": number, "max": number}
if len(self.min_max_stack):
last_min_max = self.min_max_stack[len(self.min_max_stack) - 1]
new_min_max["min"] = min(last_min_max["min"], number)
new_min_max["max"] = max(last_min_max["max"], number)
self.min_max_stack.append(new_min_max)
self.stack.append(number)
# O(1) time | O(1) space
def getMin(self):
return self.min_max_stack[len(self.min_max_stack) - 1]["min"]
# O(1) time | O(1) space
def getMax(self):
return self.min_max_stack[len(self.min_max_stack) - 1]["max"]