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

minj-j·2022년 8월 28일
0

CodingTest

목록 보기
8/14

🙄 참고해서 푼 풀이

import heapq

def solution(operations):
    heap = []
    answer = []

    for i in operations:
        op = i.split()
        if op[0] == "I":
            heapq.heappush(heap, int(op[1]))
        else:
            if len(heap) == 0:
                pass
            elif op[1] == "-1":
                heapq.heappop(heap)
            else:
                heap = heapq.nlargest(len(heap), heap)[1:]
                heapq.heapify(heap)

    if heap:
        answer.append(max(heap))
        answer.append(min(heap))
    else:
        answer.append(0)
        answer.append(0)
    return answer

🌌 문제풀이 흐름

| 붙은 문자들 속 숫자들은 무조건 넣어야 하고 -1 1에 따라 min max 구분하면 되겠다.
만약 문자가 하나있는 경우에 -1, 1을 만나면 무조건 pop시켜 주기로

💛 참고한 곳들

문제풀이 참고한 블로그

profile
minj-j`s Development diary!

0개의 댓글