LeetCode | 0215. Kth Largest Element in an Array数组中的第K个最大元素【Python】

LeetCode 0215. Kth Largest Element in an Array数组中的第K个最大元素【Medium】【Python】【快排】【堆】

Problem

LeetCode

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Example 1:

Input: [3,2,1,5,6,4] and k = 2
Output: 5

Example 2:

Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4

Note:
You may assume k is always valid, 1 ≤ k ≤ array‘s length.

问题

力扣

在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。

示例 1:

输入: [3,2,1,5,6,4] 和 k = 2
输出: 5

示例 2:

输入: [3,2,3,1,2,4,5,5,6] 和 k = 4
输出: 4

说明:

你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的长度。

思路

法一:快排

找出第 k 个最大的元素,可以看成将数组从大到小排序,取第 k 个位置的元素。选择使用快排,即当分界点的索引是 k-1 时,就是第 k 个最大元素。详情可以参考耶鲁大学关于QuickSelect算法的介绍

时间复杂度: O(n)
空间复杂度: O(1)

法二:堆

构建最大堆,从上往下,第 k 个位置就是第 k 个最大元素。

时间复杂度: O(nlogk)
空间复杂度: O(k)

Python代码

# 法一:快排
class Solution(object):
    def findKthLargest(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        low, high = 0, len(nums)-1
        while low <= high:
            pivot = self.partition(nums, low, high)
            if pivot == k - 1:  # 第 k 大, 即从大到小排序第 k-1 位置(从 0 开始计算)
                return nums[pivot]
            if pivot < k - 1:
                low = pivot + 1
            else:
                high = pivot - 1

    # 划分函数
    def partition(self, nums, low, high):
        pivot_value = nums[high]  # 因为是从大到小排序, 所以选 nums[high] 为基值
        index = low
        for i in range(low, high):
            if nums[i] >= pivot_value:
                nums[i], nums[index] = nums[index], nums[i]
                index += 1
        nums[index], nums[high] = nums[high], nums[index]
        return index  # 返回的 index 即分界点

# 法二:堆
class Solution(object):
    def findKthLargest(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        # 直接调用 Python 的 heapq 模块
        import heapq
        list_k = heapq.nlargest(k, nums)
        return list_k.pop()

题外话

其实用 Python 自带的 sorted() 函数排序也能 AC。

return sorted(nums)[-k]  # 用 Python 自带的排序算法一行代码就能 AC

代码地址

GitHub链接

原文地址:https://www.cnblogs.com/wonz/p/12309686.html

时间: 2024-12-11 13:24:34

LeetCode | 0215. Kth Largest Element in an Array数组中的第K个最大元素【Python】的相关文章

215 Kth Largest Element in an Array 数组中的第K个最大元素

在未排序的数组中找到第 k 个最大的元素.请注意,它是数组有序排列后的第 k 个最大元素,而不是第 k 个不同元素.例如,给出 [3,2,1,5,6,4] 和 k = 2,返回 5.注意事项:你可以假设 k 总是有效的,1 ≤ k ≤ 数组的长度. 详见:https://leetcode.com/problems/kth-largest-element-in-an-array/description/ 方法一: class Solution { public: int findKthLarges

[LeetCode] Kth Largest Element in an Array 数组中第k大的数字

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array'

网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5,2,2],5,3 返回:2 note: 注意手写快排的时候: while(i < j) { while(j > i && a[j] > a[left]) j--; while(i < j && a[i] <= a[left]) i++; if(i

LeetCode 215 : Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example,Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array's

Java for LeetCode 215 Kth Largest Element in an Array【Coming Soon】

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. 解题思路: 本题是<算法导论>原题,

【leetcode】Kth Largest Element in an Array (middle)☆

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example,Given [3,2,1,5,6,4] and k = 2, return 5. 思路: 堆.讲解:二叉堆 class Solution { public: //新插入i结点 其父节点为(i

leetcode 215. Kth Largest Element in an Array 寻找第k个大的数---------- java

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example,Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array's

【leetcode】Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example,Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array's

leetcode 215 Kth Largest Element in an Array : 快排

在无序的数组中找到第k大的元素,也就是若长度为n的数组从小到大排列时,下标为n-k的元素. 注意Example2:第4大的元素是4,也就是数组中出现的两个5分别是第2大和第3大的数字. 解法一:直接将数组从大到小排序,取出下标为n-k的元素. class Solution { public: int findKthLargest(vector<int>& nums, int k) { sort(nums.begin(), nums.end()); return nums[nums.si