본문 바로가기

Coding Test/Programmers

[프로그래머스/파이썬] 이중우선순위큐

728x90

 

 

나의 풀이

import heapq

def solution(operations):
    heap = []

    while operations:
        cmd, num = operations.pop(0).split(' ')
        if cmd == 'I':
            heapq.heappush(heap, int(num))
        elif heap:
            if num == '1':
                heap = heapq.nsmallest(len(heap) - 1, heap)
                heapq.heapify(heap)
            elif num == '-1':
                heapq.heappop(heap)

    if heap:
        return [heapq.nlargest(1, heap)[0], heap[0]]
    else:
        return [0, 0]

 

반응형