I number : insert number to the q
<br/> D 1 : Delete the max value from heapq
<br/> D -1: Delete the min value from heapq
这是条件
,所以当
[I 16,D 1] return [0,0]
[I 7,I 5,I -5,D -1] return [7,5]
我用这种方式尝试过,但是某些测试用例没有通过
,为什么要修复此代码,如果有更好的方法,请告诉我
import heapq
def solution(operations):
answers = []
answer_max = []
answer_min = []
arr_max = []
arr_min = []
try:
for ope in operations:
o,n=ope.split(" ")
n = int(n)
if o=='I':
heapq.heappush(answer_max,(n,n))
heapq.heappush(answer_min,(-n,n))
elif o=='D':
if n==-1:
if len(answer_min)==0: continue
else:
if len(answer_min)==1:heapq.heappop(answer_min)
heapq.heappop(answer_max)
elif n==1:
if len(answer_max)==0: continue
else:
if len(answer_max)==1: heapq.heappop(answer_max)
heapq.heappop(answer_min)
arr_min = [a[1] for a in answer_min]
arr_max = [a[1] for a in answer_max]
arr_max = [a for a in arr_max if a in arr_min]
answers = [arr_min[0], arr_max[0]]
return answers
except: return [0,0]