-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogrammers_heap_3.py
30 lines (29 loc) · 982 Bytes
/
programmers_heap_3.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
import heapq
def solution(operations) :
answer = []
maxheap = []
minheap = []
for i in range(len(operations)) :
operator, number = operations[i].split(" ")
num = int(number)
if operator == "I" :
heapq.heappush(minheap, num)
heapq.heappush(maxheap, (-num, num))
elif operator == "D" :
if num == 1 :
if(len(maxheap) != 0) :
max = heapq.heappop(maxheap)[1]
minheap.remove(max)
elif num == -1 :
if(len(minheap) != 0) :
min = heapq.heappop(minheap)
maxheap.remove((-min, min))
if len(minheap) == 0 :
answer.append(0)
answer.append(0)
else :
answer.append(heapq.heappop(maxheap)[1])
answer.append(heapq.heappop(minheap))
return answer
operations = ["I 16", "I -5643", "D -1", "D 1", "D 1", "I 123", "D -1"]
print(solution(operations))