코딩테스트
백준 최대힙 11279
choyou831
2024. 8. 9. 16:18
import sys
import heapq
n = int(input())
heap = []
for i in range(n):
a = int(sys.stdin.readline())
if a == 0:
if heap:
print((-1)*heapq.heappop(heap))
else:
print(0)
else:
heapq.heappush(heap,(-1)*a)
파이썬 힙 모듈을 사용해주었다. 힙은 heappush,heappop은 최소힙에서만 작용하기때문에 -1을 곱해주어 가장큰 값을 가장 작은값으로 바꾸어 주었다.