λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
🧩 Algorithm/μš°μ„ μˆœμœ„ 큐

[λ°±μ€€] 1927번: μ΅œμ†Œ νž™

by HelloRabbit 2023. 2. 28.
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)   # νž™μ— 숫자 λ„£κΈ°

 

 

 

λŒ“κΈ€