파이썬 알고리즘

백준 1927 최소 힙

나미-IT 2022. 1. 26. 19:17

 - 최소 힙이 기본, import  heapq

 - heappush  heappop 

 

import heapq, sys
N = int(sys.stdin.readline())
heap = []
for _ in range(N):
    x = int(sys.stdin.readline())
    if x == 0:
        if len(heap) == 0:
            print(0)
        else:
            print(heapq.heappop(heap))
            
    else:
        heapq.heappush(heap,x)

 

input()썼는데 시간초과 나면 해당부분만 sys.stdin.readline()로 대체

 

기본함수

import heapq
heap=[]
heapq.heappush(heap,n)
heapq.heappop(heap)