파이썬 문제풀이
[백준 파이썬] 1927 최소 힙
냄비짱
2023. 8. 21. 18:58
728x90
📌문제 출처
백준 단계별 문제풀이 - 우선순위 큐
https://www.acmicpc.net/problem/1927
1927번: 최소 힙
첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0
www.acmicpc.net
❓ 문제
❗ 풀이
- heap 자료구조 사용
📗 풀이 코드
import sys
from heapq import heappush, heappop
input = sys.stdin.readline
nums = []
for _ in range(int(input())):
i = int(input())
if i :
heappush(nums,i)
else :
if not nums:
print(0)
else :
print(heappop(nums))
📗 코드 해설
- heappush로 nums라는 list에 원소를 추가하며 자료구조를 heap으로 변경
- heappop으로 최소값을 출력