[DS] 1927번 - 최소 힙(40일차)

bob.sort·2021년 7월 14일
0
post-thumbnail
#코드 실행 시간 단축
import sys
#최소 힙을 사용하기 위한 heapq 모듈
import heapq
input = sys.stdin.readline

#최소 힙 list 선언
heap_arr = []

for i in range(int(input())):
  #정수 입력
  x = int(input())
  #정수가 0이 아닐 때
  if(x != 0):
    #최소 힙에 정수 추가
    heapq.heappush(heap_arr, x)
  #정수가 0일 때
  else:
    #최소 힙이 비어 있지 않으면
    if(heap_arr):
      #최소값 제거 후 출력
      print(heapq.heappop(heap_arr))
    #최소 힙이 비어 있으면
    else:
      #0 출력
      print(0)

#인사이트
#파이썬은 모듈만 잘 써도 쉽다
profile
Interest in Computer Graphics and Computer Vision

0개의 댓글