π§© Algorithm/μ°μ μμ ν
[λ°±μ€] 1927λ²: μ΅μ ν
HelloRabbit
2023. 2. 28. 23:13
728x90
Hint
1. ν μλ£κ΅¬μ‘° μ΄μ©νκΈ° (νμ΄λ?)
2. νμμ heappop()μ νλ©΄ κ°μ₯ μμ κ°μ΄ λ°ν λ¨
λ°±μ€ 1927λ²: μ΅μ ν
import heapq
import sys
n = int(sys.stdin.readline())
heap = [] # ν λ§λ€κΈ°(리μ€νΈλ λ§λλ λ²μ λκ°μ)
for i in range(n):
num = int(sys.stdin.readline())
if num == 0: # input μ«μκ° 0 μ΄λ©΄ νμ¬ νμ λ€μ΄μλ μ΅μκ° μΆλ ₯νκΈ°
print(heapq.heappop(heap) if heap != [] else 0) # νμ΄ λΉμ΄μμ§ μμ λλ§ heappop() νκΈ°
else:
heapq.heappush(heap, num) # νμ μ«μ λ£κΈ°