題目描述
輸入n個整數(shù)局服,找出其中最小的K個數(shù)罐孝。例如輸入4,5,1,6,2,7,3,8這8個數(shù)字县匠,則最小的4個數(shù)字是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])