• 数组中的第K个最大元素 -- 堆&快排


    题目描述

    LeetCode链接:数组中的第K个最大元素

    给定整数数组 nums 和整数 k,请返回数组中第 k 个最大的元素。
    请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。
    你必须设计并实现时间复杂度为 O(n) 的算法解决此问题。

    在这里插入图片描述

    class FindKthLargest:
        """
        https://leetcode.cn/problems/kth-largest-element-in-an-array/
        215. 数组中的第K个最大元素
        """
    
        def solution1(self, k: int, nums: List[int]):
            """
            小根堆
            构建一个大小为K的小根堆,放入K个元素。遍历元素,大于堆顶加入堆,维护堆的大小为k。
            最后堆顶即可第k大元素。
            时间复杂度:O(N*logK)
            空间复杂度:O(K)
            :param k:
            :param nums:
            :return:
            """
            min_heap = []
            for num in nums:
                heapq.heappush(min_heap, num)
                if len(min_heap) > k:
                    heapq.heappop(min_heap)
            return min_heap[0]
    
        def solution2(self, k: int, nums: List[int]):
            """
            大根堆
            所有元素都放入堆,最后pop()k次即可。
            时间复杂度:O(N*logN)
            空间复杂度:O(N)
            :param k:
            :param nums:
            :return:
            """
            max_heap = []
            # 所有元素都放入堆
            for num in nums:
                heapq.heappush(max_heap, -num)
    
            res = None
            for _ in range(k):
                res = -heapq.heappop(max_heap)
            return res
    
        def solution3(self, k: int, nums: List[int]):
            """
            快排
            时间复杂度:O(N)
            空间复杂度:O(N)
            :return:
            """
            return self.findK(nums, k, 0, len(nums)-1)
    
        def findK(self, nums, k, low, high):
            if low == high:
                return nums[low]
    
            partition = self.quickSortOneTime(nums, low, high)
            # 找到的是第几个大值
            idx = high - partition + 1
            if idx == k:
                return nums[partition]
            elif idx < k:
                return self.findK(nums, k-idx, low, partition-1)
            else:
                return self.findK(nums, k, partition+1, high)
    
        def quickSortOneTime(self, nums, low, high):
            """
            一趟快速排序,即【快速选择】
            :param nums:
            :param low:
            :param high:
            :return:
            """
            k_value = nums[low]
            while low < high:
                while k_value < nums[high] and low < high:
                    high -= 1
                nums[low] = nums[high]
    
                while k_value >= nums[low] and low < high:
                    low += 1
                nums[high] = nums[low]
    
            nums[high] = k_value
            return high
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
  • 相关阅读:
    1.vue的响应式原理
    【react】fiber解决了什么问题,及diff算法
    MATLAB程序设计与应用 4.4 特殊形式的函数
    [附源码]java毕业设计旅游管理系统
    CSP-J/S第二轮认证注意事项
    抖音众多接口展示
    Cards
    SQL的编写需要注意的问题
    面试:Glide和Fresco图片框架对比
    Math对象
  • 原文地址:https://blog.csdn.net/qq_32275289/article/details/126448516