최대 1 분 소요

Heap

[BOJ 11279] 최대 힙

문제

최대 힙

구조화

예제 시각화

해답

import heapq as hq
import sys
input = sys.stdin.readline
n=int(input())
max_heap = []
hq.heapify(max_heap)
cmd = []
for i in range(n):
    cmd.append(int(input()))
for i in cmd:
    if i==0:
        if len(max_heap)==0: # empty case
            print(0)
        else:
            target = hq.heappop(max_heap) # root 노드 제거 
            print(target[1]) # 출력
    else:
        hq.heappush(max_heap,(-i,i)) # 음수를 통해 max heapify

댓글남기기