백준 11279 최대 힙

coffeed-cat·2021년 7월 7일
0

알고리즘

목록 보기
10/11

✅ 백준 11279 최대 힙

정답.

파이썬이라 그런지 매우 쉬웠다.

heapq 모듈 써서, 숫자 넣을 때랑 뺄 때 둘다 -1 곱해주면 최대 힙처럼 쓸 수 있다.

import sys
import heapq

n = int(sys.stdin.readline())

heap = []

for i in range(n) :
    num = int(sys.stdin.readline())
    if num == 0 :
        if len(heap) == 0 :
            print(0)
            continue
        print(-1*heapq.heappop(heap))
        continue
    heapq.heappush(heap,-1*num)
profile
공부중

0개의 댓글