leetcode215. 数组中的第K个最大元素

1) 使用优先级队列。PriorityQueue  容量为k。遍历到最后,就是queue中头节点。

class Solution {
    public int findKthLargest(int[] nums, int k) {
            PriorityQueue<Integer> queue = new PriorityQueue();
            for(int i = 0; i < nums.length; i++){
                if(queue.size() < k){
                    queue.add(nums[i]);
                }else{
                    if(nums[i] > queue.peek()){
                        queue.poll();
                        queue.add(nums[i]);
                    }
                }
            }
            return queue.peek();
            }
}

时间O(nlogn)、空间O(n)

2)基于快速排序。

在分解的过程当中,我们会对子数组进行划分,如果划分得到的q正好就是我们需要的下标,就直接返回a[g];否则,如果q比目标下标小,就递归右子区间,否则递归左子区间。这样就可以把原来递归两个区间变成只递归一个区间,提高了时间效率。
我们知道快速排序的性能和「划分」出的子数组的长度密切相关。直观地理解如果每次规模为n的问题我们都划分成1和n一1,每次递归的时候又向n-1的集合中递归,这种情况是最坏的,时间代价是o(n^2)。我们可以引入随机化来加速这个过程,它的时间代价的期望是O(n),证明过程可以参考「《算法导论》9.2:期望为线性的选择算法」。

class Solution {
    Random random = new Random();

    public int findKthLargest(int[] nums, int k) {
        return quickSelect(nums, 0, nums.length - 1, nums.length - k);
    }

    public int quickSelect(int[] a, int l, int r, int index) {
        int q = randomPartition(a, l, r);
        if (q == index) {
            return a[q];
        } else {
            return q < index ? quickSelect(a, q + 1, r, index) : quickSelect(a, l, q - 1, index);
        }
    }

    public int randomPartition(int[] a, int l, int r) {
        int i = random.nextInt(r - l + 1) + l;
        swap(a, i, r);
        return partition(a, l, r);
    }

    public int partition(int[] a, int l, int r) {
        int x = a[r], i = l - 1;
        for (int j = l; j < r; ++j) {
            if (a[j] <= x) {
                swap(a, ++i, j);
            }
        }
        swap(a, i + 1, r);
        return i + 1;
    }

    public void swap(int[] a, int i, int j) {
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
}


 

 public static void main(String[] args) {
        int[] testArray = new int[]{3,1,2,4};
        System.out.println(findKthLargest(testArray,2));

    }
    public static int findKthLargest(int[] nums, int k) {
        return getK(nums,k,0,nums.length-1);
    }
    public static int getK(int[] nums,int k,int begin,int end){
        int i = partition(nums,begin,end);
        if((nums.length - i) == k){
            return nums[i];
        }else if((nums.length - i)>k){
            return  getK(nums,k,i+1,end);
        }else{
            return  getK(nums,k,begin,i-1);
        }
    }
    private static int partition(int[] arr,int begin,int end){
        int tar = (int) (Math.random()*(end - begin)) + begin;
        int left = begin;
        int right = end;
        while (left < right){
            while (left <= end && arr[left] <= arr[tar]) left++;
            while (right >= begin && arr[right] > arr[tar]) right--;
            if(left < right){
                swap(arr,left,right);
            }
        }
        swap(arr,tar,right);
        return right;
    }
    public static void swap(int[] arr,int a,int b){
        int tem = arr[a];
        arr[a] = arr[b];
        arr[b] = tem;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

失忆机器

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值