[백준] 11279. 최대 힙

원숭2·2022년 1월 26일
0

백준

목록 보기
15/54

문제

풀이

  1. heapq 모듈은 최소 힙만을 지원하므로, 입력값에 -를 붙이면 최대 힙 처럼 사용 가능함.

코드

import heapq
import sys

def solution() :
    n = int(sys.stdin.readline())
    heap = []
    
    for _ in range(n) :
        t = int(sys.stdin.readline())
        if t == 0 :
            if len(heap) == 0 :
                print(0)
            else :
                print(-heapq.heappop(heap))
        else :
            heapq.heappush(heap, -t)

solution()

0개의 댓글