공부, 기록

프로그래머스 이중우선순위큐 파이썬(PYTHON) 본문

코딩

프로그래머스 이중우선순위큐 파이썬(PYTHON)

무는빼주세요 2020. 11. 14. 14:09

문제링크 : programmers.co.kr/learn/courses/30/lessons/42628

 

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

 

programmers.co.kr

 

import heapq
def solution(operations):
    operlist = list(operations)
    hq=[]
    answer =[]
    for i in operlist:
        oper,num = i.split(" ")
        if oper == "I":
            heapq.heappush(hq,int(num))
        elif oper =="D":
            if len(hq)!=0:
                if num == "1":
                    hq.pop(-1)
                elif num == "-1":
                    heapq.heappop(hq)
    if len(hq) == 0:
        answer =[0,0]
    else:
        answer.append(max(hq))
        answer.append(min(hq))
    return answer