명령어를 분기해서 큐 연산을 하는 간단한 문제이다.
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