-
Notifications
You must be signed in to change notification settings - Fork 0
/
154.py
50 lines (34 loc) · 1.07 KB
/
154.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
46
47
48
49
50
"""
Problem:
Implement a stack API using only a heap. A stack implements the following methods:
push(item), which adds an element to the stack
pop(), which removes and returns the most recently added element (or throws an error if
there is nothing on the stack)
Recall that a heap has the following operations:
push(item), which adds a new key to the heap
pop(), which removes and returns the max value of the heap
"""
from sys import maxsize
from DataStructures.Heap import MinHeap
class Stack:
def __init__(self) -> None:
self.heap = MinHeap()
self.next_wt = maxsize
def pop(self) -> int:
if len(self.heap) == 0:
raise ValueError("Stack Underflow")
_, val = self.heap.extract_min()
return val
def push(self, val: int) -> None:
self.heap.insert((self.next_wt, val))
self.next_wt -= 1
if __name__ == "__main__":
stack = Stack()
stack.push(1)
stack.push(7)
stack.push(4)
print(stack.pop())
stack.push(2)
print(stack.pop())
print(stack.pop())
print(stack.pop())