给定一个长度为 n 的可能有重复值的数组,找出其中不去重的最小的 k 个数。例如数组元素是4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4(任意顺序皆可)。
数据范围:,数组中每个数的大小
要求:空间复杂度 ,时间复杂度
[4,5,1,6,2,7,3,8],4
[1,2,3,4]
返回最小的4个数即可,返回[1,3,2,4]也可以
[1],0
[]
[0,1,2,1,2],3
[0,1,1]
class Solution: def GetLeastNumbers_Solution(self , input: List[int], k: int) -> List[int]: # write code here input.sort() c=Counter(input) res=defaultdict() a=1 reb=[] for i in c.items(): a+=1 res[i[0]]=i[1] if a>k: break for j in res.items(): for i in range(j[1]): reb.append(j[0]) return reb[:k]
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param input int整型一维数组 # @param k int整型 # @return int整型一维数组 # class Solution: def GetLeastNumbers_Solution(self , input: List[int], k: int) -> List[int]: # write code here res = [] for _ in range(k): minre = min(input) input.remove(minre) res.append(minre) return res
class Solution: def GetLeastNumbers_Solution(self , input: List[int], k: int) -> List[int]: if k == 0: return [] list_min = input[:k] for i in range(k, len(input)): val = input[i] val_max = max(list_min) if val < val_max: ind = list_min.index(val_max) list_min[ind] = val list_min.sort() return list_min
class Solution: # 实现堆排序 def sink(self, input, i, n): j=2*i+1 while j<n: if j<n-1 and input[j]>input[j+1]: j+=1 if input[j]<input[i]: input[i], input[j]=input[j], input[i] i=j j=2*i+1 def GetLeastNumbers_Solution(self, input: List[int], k: int) -> List[int]: if len(input)==0: return [] n=len(input) for i in range((n-1)//2, -1, -1): self.sink(input, i, n) ans=[] for _ in range(min(k, n)): ans.append(input[0]) input[0]=input[-1] input.pop() self.sink(input, 0, len(input)) return ans
import heapq class Solution: def GetLeastNumbers_Solution(self, input: List[int], k: int) -> List[int]: heapq.heapify(input) return heapq.nsmallest(k, input)
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param input int整型一维数组 # @param k int整型 # @return int整型一维数组 # class Solution: def __init__(self): """构建小根堆""" self.heap = [] def push(self, num: int): tmp = self.heap tmp.append(num) child = len(tmp) - 1 # 平衡小根堆 while child >0: parent = (child-1)//2 if tmp[parent] > tmp[child]: tmp[parent], tmp[child] = tmp[child], tmp[parent] child = parent else: break def pop(self): tmp = self.heap tmp[0], tmp[-1] = tmp[-1], tmp[0] ret = tmp.pop() # 平衡小根堆 parent = 0 while parent < len(tmp): child = parent * 2 + 1 if child >= len(tmp): break if child + 1 < len(tmp) and tmp[child] > tmp[child+1]: child = child + 1 if tmp[child] < tmp[parent]: tmp[child], tmp[parent] = tmp[parent], tmp[child] parent = child else: break return ret def GetLeastNumbers_Solution(self , input: List[int], k: int) -> List[int]: ret = [] if len(input) < k: return ret for i in input: self.push(i) for i in range(k): min = self.pop() ret.append(min) return ret
class Solution: def GetLeastNumbers_Solution(self , inputs: List[int], k: int) -> List[int]: import heapq#小顶堆 res = [] if len(inputs)>=k and k>0: q=[] for i in range(k):#初始化大顶堆,默认小顶推 heapq.heappush(q,-inputs[i])#绝对值大的在上面,返回其相反数即可 for i in range(k,len(inputs)):#剩余元素入堆 #每次与大顶推的第一个比,如果比最小的k个中的最大值小,那就加入 if -q[0] > inputs[i]:#-q[0]就是一个正数,最小的k个里面的最大的 heapq.heapreplace(q,-inputs[i]) #先弹出当前的最小元素,而后将-inputs[i]插入到heapq序列当中 for i in range(k): res.append(-q[0])#每次取堆顶元素的相反数,也就是最小的k个中最大的那个 heapq.heappop(q)#从heapq序列中弹出第一个元素(最小值) return res
# -*- coding:utf-8 -*-
class Solution:
def GetLeastNumbers_Solution(self, tinput, k):
# write code here
if not tinput:
return []
return self.partition(0, len(tinput)-1,tinput,k)
def partition(self, start, end, tinput, k):
if start > end:
return tinput[:k]
left, right = start, end
pivot = tinput[(start + end)//2]
while left <= right:
while left <= right and tinput[left] < pivot:
left += 1
while left <= right and tinput[right] > pivot:
right -= 1
if left <= right:
tinput[left], tinput[right] = tinput[right], tinput[left]
left += 1
right -= 1
if k <= right:
self.partition(start, right, tinput, k)
if k >= left:
self.partition(left, end, tinput, k)
return tinput[:k]
# -*- coding:utf-8 -*- class Solution: def GetLeastNumbers_Solution(self, tinput, k): # write code here return sorted(tinput)[:k]
class Solution: def quicksort(self,nums): if len(nums)<2: return nums else: flag=nums[0] less=[i for i in nums[1:] if i <flag] more=[i for i in nums[1:] if i>= flag] return self.quicksort(less)+[flag]+self.quicksort(more) def GetLeastNumbers_Solution(self, tinput, k): if k>len(tinput): return [] else: alist=self.quicksort(tinput) return alist[:k]