Algorithm/Baekjoon

[백준 1927] 최소 힙 (Python)

문제 링크 #

문제 해설 #

Idea #

  • Heapq
  • 파이썬 heapq 모듈 자체가 최소힙이기 때문에 해당하는 기능을 활용하여 구현

Time Complexity #

  • O(Log N) = 16

Data Size #

  • N: 1 <= int <= 100,000
  • x: 0 <= int < 2^31

해설 코드 #

python
import heapq
import sys
input = sys.stdin.readline

N = int(input())
arr = list()

for _ in range(N):
    x = int(input())
    if x > 0:
        heapq.heappush(arr, x)
    else:
        if arr:
            print(heapq.heappop(arr))
        else:
            print(0)
PREV [백준 15686] 치킨 배달 (Python) NEXT [백준 1780] 종이의 개수 (Python)