[백준] 11279번: 최대 힙 문제 풀이 파이썬

현톨·2022년 4월 15일
0

Algorithm

목록 보기
23/42

문제 링크

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

풀이 방식

  1. 힙큐 자료구조를 사용한다.
  2. 최소순으로 정렬되는 힙큐 자료구조를 최대힙으로 바꿔준다.
  3. 힙큐 자료구조에 정수값과 정수값의 역수를 함께 넣어 역수 기준으로 정렬되도록 한다.

전체 코드

import heapq
from sys import stdin

N = int(stdin.readline().rstrip())
heap = []

for _ in range(N):
    x = int(stdin.readline().rstrip())
    if x:
        heapq.heappush(heap, [-x, x])
    else:
        if heap:
            print(heapq.heappop(heap)[1])
        else:
            print(0)
profile
기록하는 습관 들이기

0개의 댓글