기타
프로그래머스 파이썬 이중우선순위큐
작지
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