본문 바로가기

알고리즘/프로그래머스(Python)

힙 - 이중우선순위큐 / 파이썬

import heapq

def reset(heap):
    temp = []
    for h in heap:
        heapq.heappush(temp, -h)
    return temp

def solution(operations):
    min_heap, max_heap = [], []
    for oper in operations:
        op, num = oper.split()
        num = int(num)
        if op == 'I':
            heapq.heappush(min_heap, num)
            heapq.heappush(max_heap, -num)
        elif min_heap and op == 'D':
            if num == 1:
                h = heapq.heappop(max_heap)
                min_heap = reset(max_heap)
            else:
                heapq.heappop(min_heap)
                max_heap = reset(min_heap)
    if min_heap:
        return [-heapq.heappop(max_heap), heapq.heappop(min_heap)]
    else:
        return [0, 0]

 

 

programmers.co.kr/learn/courses/30/lessons/42628

 

코딩테스트 연습 - 이중우선순위큐

 

programmers.co.kr