import heapq
def reset(heap):
temp = []
for h in heap:
heapq.heappush(temp, -h)
return temp
def solution(operations):
min_heap, max_heap = [], []
for oper in operations:
op, num = oper.split()
num = int(num)
if op == 'I':
heapq.heappush(min_heap, num)
heapq.heappush(max_heap, -num)
elif min_heap and op == 'D':
if num == 1:
h = heapq.heappop(max_heap)
min_heap = reset(max_heap)
else:
heapq.heappop(min_heap)
max_heap = reset(min_heap)
if min_heap:
return [-heapq.heappop(max_heap), heapq.heappop(min_heap)]
else:
return [0, 0]
programmers.co.kr/learn/courses/30/lessons/42628
'알고리즘 > 프로그래머스(Python)' 카테고리의 다른 글
그리디 - 조이스틱 / 파이썬 (0) | 2020.11.09 |
---|---|
그래프 - 가장 먼 노드 / 파이썬 (0) | 2020.11.08 |
힙 - 디스크 컨트롤러 / 파이썬 (0) | 2020.11.05 |
힙 - 더 맵게 / 파이썬 (0) | 2020.11.05 |
큐 - 프린터 / 파이썬 (0) | 2020.11.04 |