[알고리즘]99클럽 코테 스터디 TIL + 프로그래머스 이중우선순위큐

Lee Yongin·2024년 5월 25일
1

알고리즘

목록 보기
12/14

문제설명

명령어를 분기해서 큐 연산을 하는 간단한 문제이다.

아이디어

heapq 라이브러리를 이용하면 최솟값, 최댓값 연산을 간단하게 할 수있다.
최소 힙으로 구성해서 최솟값 삭제는 heappop()으로, 최댓값 삭제는 리스트 pop() 연산으로 해결하면 되기 때문이다.

코드

import heapq
def solution(operations):
    answer = []
    heapq.heapify(answer)
    for i in operations:
        str = i
        if str == "D -1":
            if answer:
                heapq.heappop(answer)
        elif str == "D 1":
            
            if answer:
                answer.pop()
        else:
            num = int(str[2:])
            heapq.heappush(answer, num)
    
    result = [0,0]
    if answer:
        result[0] = max(answer)
        result[1] = min(answer)
    return result
profile
⚡실력으로 말하는 개발자가 되자⚡p.s.기록쟁이

0개의 댓글