Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags more
Archives
Today
Total
관리 메뉴

작은 지식주머니

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

기타

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

작지 2021. 12. 7. 14:06

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

 

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

 

programmers.co.kr

 

 

plus 와 minus 경우 두 가지의 힙큐를 넣고 최대값과 최소값을 빼서 출력

 

백준에서는 그냥 시간초과로 튕겨나갈 방법이지만 여기선 그냥 된다.

배열이 작아서 그런가?

import heapq

def solution(operations):

    plus_h = []
    minus_h = []

    for i in operations:
        a,b = i.split()
        b = int(b)
        if a == "I":
            heapq.heappush(plus_h,-b)
            heapq.heappush(minus_h,b)
        elif a == "D":
            if b == 1 and len(plus_h) > 0:
                minus_h.remove(-heapq.heappop(plus_h))
            elif b == -1 and len(plus_h) > 0:
                plus_h.remove(-heapq.heappop(minus_h))


    if len(minus_h) == 0:
        answer = [0,0]
    else:
        answer = [-plus_h[0],minus_h[0]]

    return answer

 

Comments