import heapq
def solution(operations):
heap = []
for i in operations:
if i == "D -1":
if heap:
heapq.heappop(heap)
elif i == "D 1":
if heap:
heap.remove(max(heap))
else:
temp = i.split()
heapq.heappush(heap, int(temp[1]))
if heap:
return [max(heap),min(heap)]
else:
return [0,0]
level3 중에서 쉬운 축에 끼는 문제라고 생각한다. 자료구조 중에서 힙을 사용하여 문제 그대로 구현하면 되는 문제이다. 코드를 보면 heapq를 import하여 heap이라는 리스트를 만들어 놓고 operations원소가 D -1일때는 최솟값을 pop해주고 D 1일때는 최댓값을 pop해준다. 그게 아닌 I 숫자 가 나온다면 split으로 나눈뒤에 숫자를 int로 명시적 형변환을 하여 heap리스트를 push해준다. 반복문이 끝낱을 때 heap에 원소가 없으면 [0,0] 그게 아니라면 [heap최댓값, heap최솟값]을 return 한다.