[파이썬] 백준 BOJ 1927번 최소힙

강준호·2023년 7월 4일
0

https://www.acmicpc.net/problem/1927

초고

import heapq
n = int(input())
heap = []
for i in range(n):
    x = int(input())
    if x == 0:
        if heap:
            low =heapq.heappop(heap)
        else:
            low = 0
        print(low)
    else:
        heapq.heappush(heap,x)

시간초과 발생

개선된 코드

import heapq
n = int(input())
heap = []
ans = []
for i in range(n):
    x = int(input())
    if x == 0:
        if heap:
            low =heapq.heappop(heap)
        else:
            low = 0
        ans.append(str(low))
    else:
        heapq.heappush(heap,x)

result = '\n'.join(ans)
print(result)

남들 풀이를 보면 sys.stdin.write 로 해결하더라

0개의 댓글