[Programmers / Python / 힙] - 이중우선순위큐

Young·2021년 5월 31일
0

출처 https://programmers.co.kr/learn/courses/30/lessons/42628?language=python3

처음에는 heap을 max, min으로 변환시켜가며 각 root를 삭제하려 하려 했지만 그냥 배열 sort 쓰는게 나을거 같아 아래와 같이 구현했다.

나의 풀이

import heapq

def solution(operations):
    answer = []
    queue = []
    
    for i in operations:
        ope, num = i.split(" ")[0], int(i.split(" ")[1])

        if ope == "I":
            queue.append(num) 
        else:
            if len(queue) > 0 :
                queue.sort()
                if num == 1:
                    queue.pop(-1)
                else:         
                    queue.pop(0)
    
    if len(queue) == 0:
        answer = [0,0]
    else:
        queue.sort()
        answer = [queue[-1], queue[0]]
            
    return answer
profile
👩🏻‍💻

0개의 댓글