쏘ing

[Python] 백준 1927 최소 힙 본문

알고리즘/CLASS 3

[Python] 백준 1927 최소 힙

한민민 2022. 2. 22. 09:54

https://www.acmicpc.net/problem/1927

풀이

import heapq
import sys

num = int(sys.stdin.readline().rstrip())

num_list = []

for _ in range (num):
    temp = int(sys.stdin.readline().rstrip())
    
    # 입력이 0일 때
    if temp == 0:
    	# 배열이 비어있을 경우 0 출력
        if len(num_list) == 0:
            print(0)
        # 아니면 작은 값 출력
        else:
            print(heapq.heappop(num_list))
    # 0이 아닐 경우 배열에 넣기
    else:
        heapq.heappush(num_list, temp)
Comments