작은 지식주머니
프로그래머스 파이썬 이중우선순위큐 본문
링크 : 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
'기타' 카테고리의 다른 글
사내 스터디 기록 - AWS SUMMIT 2일차 (0) | 2023.07.25 |
---|---|
프로그래머스 파이썬 더 맵게 (0) | 2021.12.07 |
프로그래머스 파이썬 프린터 (0) | 2021.12.07 |
프로그래머스 파이썬 구명보트 (0) | 2021.12.07 |
프로그래머스 파이썬 가장 먼 노드 (0) | 2021.12.07 |
Comments