[백준] 11279번 최대 힙

거북이·2023년 1월 25일
0

백준[실버2]

목록 보기
15/81
post-thumbnail

💡문제접근

  • 이전 포스팅에 있었던 [[백준] 1927번 최소 힙]을 변형한 문제다.
  • heapheappush수행할 때 마이너스 부호를 붙여 넣어준 다음 가장작은 원소를 출력하는 heappop을 수행할 때 앞에 마이너스 부호를 붙여준다.

💡코드(메모리 : 37000KB, 시간 : 124ms)

import sys
input = sys.stdin.readline
import heapq

heap = []

N = int(input().strip())
for _ in range(N):
    num = int(input().strip())
    if num != 0:
        heapq.heappush(heap, -num)
    else:
        if len(heap) == 0:
            print(0)
        else:
            print(-heapq.heappop(heap))

💡소요시간 : 4m

0개의 댓글