-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathExercise_2.py
More file actions
49 lines (38 loc) · 1.49 KB
/
Exercise_2.py
File metadata and controls
49 lines (38 loc) · 1.49 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
47
48
49
#Design MinStack
# I'm using 2 stack approach, where 1st stack is saving all the items, while the 2nd is saving only
# if the new item is smaller than the last minimum item. Then during the pop we check if the removed item
# from the stack of all items is same as the min, then remove the last elemnt from 2nd stacka and set the minValue
# to be the top value from the 2nd array. This gives is O(1) time complexity for all operations and O(n) space complexity
class MinStack:
def __init__(self):
self.allItems = []
self.minOrderedItems = []
self.minItem = None
def push(self, val: int) -> None:
self.allItems.append(val)
if self.minItem == None or self.minItem >= val:
self.minItem = val
self.minOrderedItems.append(val)
# elif self.minItem >= val :
# self.minItem = val
# self.minOrderedItems.append(val)
# else:
# return
def pop(self) -> None:
poppedItem = self.allItems.pop()
if poppedItem == self.minItem:
self.minOrderedItems.pop()
if len(self.minOrderedItems) > 0:
self.minItem = self.minOrderedItems[-1]
else:
self.minItem = None
def top(self) -> int:
return self.allItems[-1]
def getMin(self) -> int:
return self.minItem
# 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()