heap
리스트를 역순으로 배열 후 값을 제거heap
리스트의 최소값과 최대값을 출력heap
리스트에 값이 남아있지 않다면 [0, 0]
을 출력📌 heap
없이도 통과되는 문제라고 한다..
이중 우선순위 큐는 다음 연산을 할 수 있는 자료구조를 말합니다.
[명령어 수신 탑(높이)]
I 숫자: 큐에 주어진 숫자를 삽입합니다.
D 1: 큐에서 최댓값을 삭제합니다.
D -1: 큐에서 최솟값을 삭제합니다.
이중 우선순위 큐가 할 연산 operations가 매개변수로 주어질 때,
모든 연산을 처리한 후 큐가 비어있으면 [0,0]
비어있지 않으면 [최댓값, 최솟값]을 return 하도록 solution 함수를 구현해주세요.
import heapq
def solution(operations):
answer = []
heap = []
for operation in operations:
command, num = operation.split()
if command == 'I':
heapq.heappush(heap, int(num))
elif command == 'D':
if heap:
if num == '1':
reverse_heap = [-val for val in heap]
heapq.heapify(reverse_heap)
heapq.heappop(reverse_heap)
heap = [-val for val in reverse_heap]
heapq.heapify(heap)
elif num == '-1':
heapq.heappop(heap)
if heap:
answer = [max(heap), min(heap)]
else:
answer = [0, 0]
return answer