剑指Offer 29. 最小的K个数 (其他)

Posted on 2018-10-15 13:59  _hqc  阅读(139)  评论(0编辑  收藏  举报

题目描述

输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4。

题目地址

https://www.nowcoder.com/practice/6a296eb82cf844ca8539b57c23e6e9bf?tpId=13&tqId=11182&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

思路

思路1:

使用冒泡排序,找出最小的k个数返回,时间复杂度O(n^2)。

思路2:

另一种O(nlogk)的算法是基于堆排序的,特别适合处理海量数据。

我们可以先创建一个大小为k的数据容器来存储最小的k个数字,接下来我们每次从输入的n个整数中的n个整数中读入一个数。如果容器中已有的数字少于k个,则直接把这次读入的整数放入容器之中;如果容器已经有k个数字了,也就是容器满了,此时我们不能再插入新的数字而只能替换已有的数字。找出这已有的k个数中的最大值,然后拿这次待插入的整数和最大值进行比较。如果待插入的值比当前已有的最大值小,则用这个数替换当前已有的最大值;如果待插入的值比当前已有的最大值还要大,那么这个数不可能是最小的k个整数之一,于是我们可以抛弃这个整数。

因此当容器满了之后,我们要做3件事情:一是在k个整数中找到最大数;二是有可能在这个容器中删除最大数;三是有可能要插入一个新的数字。如果用一个二叉树来实现这个数据容器,那么我们在O(logk)时间内实现这三步操作。因此对于n个输入数字而言,总的时间效率就是O(nlogk)。

Python

# -*- coding:utf-8 -*-
class Solution:
    def GetLeastNumbers_Solution(self, tinput, k):
        # write code here
        # 思路1 冒泡排序
        # if k < 0 or k > len(tinput):
        #     return []
        # count = k
        # for i in range(len(tinput)):
        #     min = tinput[i]
        #     for j in range(i+1,len(tinput)):
        #         if tinput[j]<min:
        #             min = tinput[j]
        #             index = j
        #     if min!=tinput[i]:
        #         tinput[i], tinput[index] = tinput[index], tinput[i]
        #     count = count-1
        #     if count == 0:
        #         break
        # return tinput[:k]
        # 思路2 堆排序
        res = []
        length = len(tinput)
        change = True
        if length <= 0 or k <= 0 or k > length:
            return res
        res = tinput[:k]
        for i in range(k, length + 1):
            if change == True:
                for j in range(0, k // 2 + 1)[::-1]:
                    self.HeadAdjust(res, j, k)
                for j in range(1, k)[::-1]:
                    res[0], res[j] = res[j], res[0]
                    self.HeadAdjust(res, 0, j)
                change = False
            if i != length and res[k - 1] > tinput[i]:
                res[k - 1] = tinput[i]
                change = True
        return res

    def HeadAdjust(self, input_list, parent, length):
        temp = input_list[parent]
        child = 2 * parent + 1
        while child < length:
            if child + 1 < length and input_list[child] < input_list[child + 1]:
                child += 1
            if temp >= input_list[child]:
                break
            input_list[parent] = input_list[child]
            parent = child
            child = 2 * parent + 1
        input_list[parent] = temp

if __name__ == '__main__':
    result = Solution().GetLeastNumbers_Solution([4, 5, 1, 6, 2, 7, 3, 8], 4)
    print(result)