-
Notifications
You must be signed in to change notification settings - Fork 234
/
min-max-stack-construction.py
45 lines (38 loc) · 1.13 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
34
35
36
37
38
39
40
41
42
43
44
45
# MIN MAX STACK CONSTRUCTION
# Feel free to add new properties and methods to the class.
class MinMaxStack:
def __init__(self):
self.minMaxStack = []
self.stack = []
# O(1) time and space
def peek(self):
# Write your code here.
return self.stack[-1]
# O(1) time and space
def pop(self):
# Write your code here.
self.minMaxStack.pop()
return self.stack.pop()
# O(1) time and space
def push(self, number):
# Write your code here.
self.stack.append(number)
if len(self.minMaxStack) == 0:
self.minMaxStack.append([number, number])
else:
currentMin = self.minMaxStack[-1][0]
currentMax = self.minMaxStack[-1][1]
if number < currentMin:
self.minMaxStack.append([number, currentMax])
elif number > currentMax:
self.minMaxStack.append([currentMin, number])
else:
self.minMaxStack.append([currentMin, currentMax])
# O(1) time and space
def getMin(self):
# Write your code here.
return self.minMaxStack[-1][0]
# O(1) time and space
def getMax(self):
# Write your code here.
return self.minMaxStack[-1][1]