bj 14235 크리스마스 선물

coh·2022년 5월 28일
1

백준

목록 보기
14/27

아.. 처음에 test case로 넣은 input값 수정 안해서 틀렸다가 두 번째에는 시간초과가 떴다. 왜 그러지 하고 살펴보니 굳이 sort를 할 필요 없는데 sort 후에 pop을 해서 시간복잡도가 최악의 경우에 n^3이었음 ㅋㅋ.. 그래서 code를 sort를 빼고 간결하게 만들어서 통과할 수 있었다.

import heapq


n = int(input())
gift = []
for _ in range(n):
    a = list(map(int, input().split()))
    if len(gift) == 0 and a[0] == 0:
        print(-1)
    elif a[0] == 0:
        print(-heapq.heappop(gift))
    else:
        for i in range(1, a[0] + 1):
            heapq.heappush(gift, -a[i])
profile
Written by coh

0개의 댓글