같은 문제지만 언어가 달라서 아직 파이썬에 적응중인 단계..
📎 최소 힙
'''
최소 힙
'''
import sys
import heapq
n = int(sys.stdin.readline())
heap = []
for i in range(n):
shot = int(sys.stdin.readline())
if shot == 0:
if heap:
print(heapq.heappop(heap))
else:
print(0)
else:
heapq.heappush(heap, shot)
# 정수를 넣는데 0이면 현재 힙에서 제일 작은 것 출력
# 아니라면 힙에 삽입
📎 최대 힙
'''
최대 힙
'''
import sys
import heapq
n = int(sys.stdin.readline())
heap = []
for i in range(n):
shot = int(sys.stdin.readline())
if shot == 0:
if heap:
print(-heapq.heappop(heap))
else:
print(0)
else:
heapq.heappush(heap, -shot)
heapq.nlargest(k, nums) 함수는 배열 nums에서 가장 큰 k개의 요소를 찾아서 리스트로 반환한다. 이 리스트는 내림차순으로 정렬된다.
heapq 모듈은 힙 큐 알고리즘, 특히 최소 힙을 기반으로 작동하는 여러 함수를 제공한다. 그러나 heapq.nlargest() 함수를 사용하여 이 경우에는 최대 힙처럼 작동하여 주어진 배열에서 k번째로 큰 값을 효율적으로 찾을 수 있다.