百度第二题 礼物最大值
百度第二题:礼物最大值,为啥用最小堆做说是超内存呢?求大佬们指点T_T,本地可以跑例子,粘到答题框里就不行了
import heapq
n,k=map(int,raw_input().split())
arr=map(int,raw_input().split())
if k>=n:
print sum(arr)
else:
l=arr[:k]
heapq.heapify(l)
i=k
res=[sum(l)]
for i in range(k,n):
if arr[i]>l[0]:
item=arr[i]
item=heapq.heapreplace(l,item)
res.append(sum(l))
m=len(res)
res=res+[res[-1]]*(n-k+1-m)
print res #百度#

查看9道真题和解析