LeetCode_Array_215. Kth Largest Element in an Array 数组中的第K个最大元素【优先队列,快速排序】【Java】【中等】

目录

一,题目描述

英文描述

中文描述

示例与说明

二,解题思路

优先队列

快速排序

三,AC代码

Java(快速排序)

四,解题过程

第一搏

第二搏

第三搏


 

一,题目描述

英文描述

Given an integer array nums and an integer k, return the kth largest element in the array.

Note that it is the kth largest element in the sorted order, not the kth distinct element.

You must solve it in O(n) time complexity.

中文描述

给定整数数组 nums 和整数 k,请返回数组中第 k 个最大的元素。

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

示例与说明

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/kth-largest-element-in-an-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

二,解题思路

优先队列

很直白的思路是,通过优先队列创建小根堆,存储前K大元素,堆顶即前K大元素中最小的元素,将所有元素过滤一遍,遇到比堆顶大的元素便替换当前堆顶。最后堆顶即最终答案;

下面第一搏也给出了不使用已有的优先队列,通过自定义siftUp和siftDown方法实现方式。

 

快速排序

快速排序中有一个partition方法,通过选择哨兵元素,将数组以哨兵为基准划分为两部分,并将哨兵放在两部分中间,这个哨兵所在位置即最终位置,如果这个位置就是k-1(以0开始),游戏结束;

通过随机选择哨兵元素可以避免数组有序时出现的极端情况。

三,AC代码

Java(快速排序)

class Solution {
    int K;
    public int findKthLargest(int[] nums, int k) {
        K = k;
        return quickSort(nums, 0, nums.length);
    }
    public int quickSort(int[] nums, int l, int r) {
        int m = partition(nums, l, r);
        if (m == K - 1) return nums[m]; // 第k大,位置在k-1
        if (m >= K) return quickSort(nums, l, m);   // 哨兵位置超过k,需要向左缩减空间
        else return quickSort(nums, m + 1, r);      // 左闭右开
    }
    public int partition(int[] nums, int l, int r) {
        int pivort = (int)(l + Math.random() * (r - l));// 左闭右开
        swap(nums, l, pivort);  // 将随机哨兵放在区间最左侧
        int index = l + 1;
        for (int i = l + 1; i < r; i++) {
            if (nums[l] < nums[i]) {    // 递减序列,大于哨兵的元素放在左侧
                swap(nums, i, index++);
            }
        }
        swap(nums, l, --index); // 将随机哨兵放到最终的位置
        return index;
    }
    public void swap(int[] nums, int i, int j) {
        int tem = nums[i];
        nums[i] = nums[j];
        nums[j] = tem;
    }
}

四,解题过程

第一搏

手撸简单的小根堆算法。一发入魂O(nlog(k))

class Solution {
    public int findKthLargest(int[] nums, int k) {
        int[] minHeap = new int[Math.min(nums.length, k)];
        int index = 0;
        // 构建堆
        for (; index < minHeap.length; index++) {
            minHeap[index] = nums[index];
            siftUp(minHeap, index);
        }

        // 将所有元素筛选一遍
        for (; index < nums.length; index++) {
            if (nums[index] > minHeap[0]) {
                minHeap[0] = nums[index];
                siftDown(minHeap, 0);
            }
        }
        return minHeap[0];
    }
    public void siftUp(int[] nums, int index) {
        int parent = (index - 1) / 2;
        while (parent >= 0 && nums[parent] > nums[index]) {
            swap(nums, parent, index);
            index = parent;
            parent = (index - 1) / 2;
        }
    }
    public void siftDown(int[] nums, int index) {
        int child = index * 2 + 1;
        while (child < nums.length) {
            if (child + 1 < nums.length && nums[child] > nums[child + 1]) {
                child += 1;
            }
            if (nums[index] > nums[child]) {
                swap(nums, index, child);
                index = child;
                child = index * 2 + 1;
            } else break;
        }
    }
    public void swap(int[] nums, int i, int j) {
        int tem = nums[i];
        nums[i] = nums[j];
        nums[j] = tem;
    }
}

第二搏

体验一把已有的优先队列使用方法

class Solution {
    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> minHeap = new PriorityQueue<>(Math.min(nums.length, k), new Comparator<Integer>(){
            public int compare(Integer a, Integer b) {
                return a - b;
            }
        });
        int index = 0;
        for (; index < k; index++) {
            minHeap.offer(nums[index]);
        }
        for (; index < nums.length; index++) {
            if (nums[index] > minHeap.peek()) {
                minHeap.poll();
                minHeap.offer(nums[index]);
            }
        }
        return minHeap.peek();
    }
}

 

第三搏

快速排序+随机哨兵,一发入魂。O(n)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值