215. Kth Largest Element in an Array

本文介绍了一种高效算法,用于从无序数组中找到第K大的数。通过使用快速排序的分区思想,实现部分排序,避免了完全排序的高时间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 length. 

从无序数组中找到第k大的数

最简单的想法就是sort 然后返回第k个 无论是Arrays.sort 还是priority queue 时间复杂度nlog(n) n是数组元素的个数


实际上我们需要的是第k大的数 相当于是需要一个部分排序 k前面的比k大 k后面的比k小 至于k前面和后面元素的顺序 是无所谓的

k前面的比k大 k后面的比k小』 看这个描述 很容易想到quick中的partition

    public int findKthLargest(int[] nums, int k) {
        k = nums.length - k;
        int start = 0, end = nums.length-1;
        while (start < end) {
            int i = partition(nums, start, end);
            if (i == k) break;
            if (i > k) {
                end = i-1;
            } else {
                start = i+1;
            }
        }
        return nums[k];
    }
    
    public int partition(int[] nums, int left, int right) {
        int small = left, big = left;
        while (big < right) {
            if (nums[big] < nums[right]) {
                swap(nums, big, small++);
            }
            big++;
        }
        swap(nums, small, right);
        return small;
    }
    
    private void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
k = nums.length - k; //原题是想找第k大的数字 但是我们通常习惯的排序是从小到大 

那么只要找第nums.length - k小的元素就可以了

然后选择数组的最后一个元素 对数组进行partition 

返回的pivot 代表的是数组以哪个元素为中间值进行了分割 如返回3 表示数组以nums[3]为中间元素做了部分排序

nums[3]前面的元素小于3 nums[3]后面的元素大于3


然后判断pivot和k的关系

pivot>k pivot后面的元素都大于pivot 不用考虑了 从前面继续部分排序

pivot<k pivot后面的元素都小于pivot 不满足条件 继续对后面部分排序





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值