-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin_of_stack.py
More file actions
44 lines (34 loc) · 966 Bytes
/
min_of_stack.py
File metadata and controls
44 lines (34 loc) · 966 Bytes
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
"""
3.2 Stack Min: How would you design a stack which, in addition to push and pop, has a function min
which returns the minimum element? Push, pop and min should all operate in 0(1) time.
"""
from linked_list import LinkedList
class Stack(object):
def __init__(self):
self.list = LinkedList()
self.curr = None
self.minVal = None
def push(self, data):
self.list.append(data)
if self.curr is None:
self.curr = self.list.head
else:
self.curr = self.curr.nxt
if not self.minVal or self.minVal > data:
self.minVal = data
def pop(self):
self.list.delete(self.curr.data)
def show(self):
self.list.show()
@property
def min(self):
return self.minVal
if __name__ == "__main__":
stack = Stack()
stack.push(10)
stack.push(20)
stack.push(0)
stack.show()
stack.pop()
stack.show()
print(stack.min)