백준 | 1927 최소 힙 [Python]

yeonk·2022년 6월 27일
0

algorithm

목록 보기
82/88
post-thumbnail

💡 Python 3






🔗 문제


1927 최소 힙 [Link]






💻 코드

처음에 input()으로 시도했는데 시간 초과가 됐다.
그래서 sys 모듈을 이용해 입력을 받았더니 통과가 되었다.
제한 시간에 유의하자.


import heapq
import sys

heap = []
n = int(sys.stdin.readline())

for _ in range(n):
    num = int(sys.stdin.readline())
    if num == 0:
        if heap: print(heapq.heappop(heap))
        else: print(0)
    else: heapq.heappush(heap, num)






참고 자료


heapq — 힙 큐 알고리즘

[Python] 파이썬 기본 모듈 - heapq

0개의 댓글