

우선순위 큐가 할 연산 operations가 매개변수로 주어질 때, 모든 연산을 처리한 후 큐가 비어있으면 [0,0] 비어있지 않으면 [최댓값, 최솟값]을 return 하도록 solution 함수를 구현
'I' 숫자 큐에 주어진 숫자를 삽입합니다.
'D 1' 큐에서 최댓값을 삭제합니다.
'D -1'큐에서 최솟값을 삭제합니다.
def solution(operations):
answer = []
queue =[]
for i in operations:
# print(i)
if i[0]=="I":
queue.append(int(i[2:]))
else:
if queue ==[]:
pass
else:
if i == "D 1":
queue.remove(max(queue))
if i == "D -1":
queue.remove(min(queue))
# print(i,queue)
if queue ==[]:
answer=[0,0]
else:
answer = [max(queue),min(queue)]
# print(answer)
return answer
from heapq import heappush, heappop
def solution(arguments):
max_heap = []
min_heap = []
for arg in arguments:
if arg == "D 1":
if max_heap != []:
heappop(max_heap)
if max_heap == [] or -max_heap[0] < min_heap[0]:
min_heap = []
max_heap = []
elif arg == "D -1":
if min_heap != []:
heappop(min_heap)
if min_heap == [] or -max_heap[0] < min_heap[0]:
max_heap = []
min_heap = []
else:
num = int(arg[2:])
heappush(max_heap, -num)
heappush(min_heap, num)
if min_heap == []:
return [0, 0]
return [-heappop(max_heap), heappop(min_heap)]
heapq 사용