题目描述
输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
# -*- coding:utf-8 -*-
import heapq
class Solution:
def GetLeastNumbers_Solution(self, tinput, k):
# write code here
if k>len(tinput): return []
pq = []
for i in tinput:
heapq.heappush(pq, -i)
if len(pq)>k: heapq.heappop(pq)
return sorted([-v for v in pq])