알고리즘

[알고리즘] 이중우선순위큐

keel_im 2021. 3. 19. 21:56
반응형

포인트

  • 힙의 특성을 이용해보자
  • 문제가 맞는지 틀린지는 잘 모르겠다. 이번, 문제는 일단 푼 정도

🧶문서는 항상 수정 될 수 있습니다. 비판은 환영합니다. 

python

import heapq

def solution(operations: list) -> list:
    answer = []
    for operations in operations:
        data = operations.split()
        if data[0] == 'I':
            heapq.heappush(answer, int(data[1]))
        elif answer and data[0] == 'D' and data[1] == '-1':
            heapq.heappop(answer)
        elif answer and data[0] == 'D' and data[1] == '1':
            answer.remove(max(answer))

    return [max(answer), min(answer)] if answer else [0, 0]


print(solution(["I 16", "D 1"]))
print(solution(["I 7", "I 5", "I -5", "D -1"]))

 

반응형