less than 1 minute read

백준 11279번 링크)


11279


import sys
import heapq
input=sys.stdin.readline
n = int(input())
h = []
for i in range(n):
    x=int(input())
    if x==0:
        if not h: # 빈 배열
            print(0)
        else:
            print(-1*heapq.heappop(h)) #'-'붙인 수들이니까 다시 '-' 붙여서 출력해주기
    else:
        heapq.heappush(h, -x)
        # heapq는 최소힙만 지원하므로 '-'를 붙인 수들을 넣어줘야함


+)

파이썬의 heapq는 최소힙만 지원한다.
그렇기 때문에 push 할때 숫자들(x)을 모두 -x 로 바꾸어 넣어주고, 출력할때만 -1를 곱해주는 방식으로 구현하였다.
추가로 if not 을 사용하면 코드를 간결하게 할 수 있다는 점을 알게되었다.
h의 사이즈가 0이 아닐 경우를 간단히 i not h: 로 표현할 수 있었다.

Categories:

Updated: