海量数字topK
import random
import heapq
n = 100
k = 10
nums = [i for i in range(n)]
random.shuffle(nums)
def topk(nums, k):
heap = []
for i in range(k):
heapq.heappush(heap, nums[i])
for i in range(k, n):
if nums[i] > heap[0]:
heapq.heappop(heap)
heapq.heappush(heap, nums[i])
return heap
print(topk(nums, k))
求最大的k个数当然用最小堆,因为要拿到最小的哪个值
时间复杂度:建堆klogk, 插入n*logk