力扣题215数组中的第K个最大元素

给定整数数组 nums 和整数 k,请返回数组中第 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

1.快速排序。并且只要找到第k大的数字就可以退出递归了。

class Solution {
    // 使用快速排序算法
    
    public int findKthLargest(int[] nums, int k) {
        quickSort(nums,0,nums.length - 1,k);
        return nums[k-1];
    }

    public void quickSort(int[] nums,int left ,int right,int k){
        if (left < right) {
            int l = left;
            int r = right;
            int currentValue = nums[l];
            while (l < r) {
                while (l < r && nums[r] < currentValue) { //从右向左找第一个小于当前值的数
                    r--;
                }

                if (l < r) {//把大的换到前面,小的换到后面
                    nums[l] = nums[r];
                    l++;
                }

                while (l < r && nums[l] > currentValue) { //从左向右找第一个小于当前值的数
                    l++;
                }

                if(l < r){//把大的换到前面,小的换到后面
                    nums[r] = nums[l];
                    r--;
                }
            }
            nums[l] = currentValue;
            if (l == k-1) { //已经找到了第K大的数字,直接返回,不用再排序子列了
                return;
            } else if (l < k - 1){ //如果当前小标小于k-1,说明在右侧,继续递归右子数组
                quickSort(nums,l + 1,right,k);
            } else { //在左侧子数组,继续递归左子数组
                quickSort(nums,left,l - 1,k);
            }
        }
    }
}

   快排可以进行优化:自己的快排就是每次选的都是数组的第一个数字作为当前要排的值,可以对当前值进行随机化。使用随机化可以加快很多。

class Solution {
    // 使用快速排序算法
    
    public int findKthLargest(int[] nums, int k) {
        quickSort(nums,0,nums.length - 1,k);
        return nums[k-1];
    }

    public void quickSort(int[] nums,int left ,int right,int k){
        if (left < right) {
            int l = left;
            int r = right;

            int random=new Random().nextInt(r - l + 1) + l;
            swap(nums,l,random);//随机快排

            int currentValue = nums[l];
            while (l < r) {
                while (l < r && nums[r] < currentValue) { //从右向左找第一个小于当前值的数
                    r--;
                }

                if (l < r) {//把大的换到前面,小的换到后面
                    nums[l] = nums[r];
                    l++;
                }

                while (l < r && nums[l] > currentValue) { //从左向右找第一个小于当前值的数
                    l++;
                }

                if(l < r){//把大的换到前面,小的换到后面
                    nums[r] = nums[l];
                    r--;
                }
            }
            nums[l] = currentValue;
            if (l == k-1) { //已经找到了第K大的数字,直接返回,不用再排序子列了
                return;
            } else if (l < k - 1){ //如果当前小标小于k-1,说明在右侧,继续递归右子数组
                quickSort(nums,l + 1,right,k);
            } else { //在左侧子数组,继续递归左子数组
                quickSort(nums,left,l - 1,k);
            }
        }    
    }

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

2.推排序。这个也一定要会。建立一个大根堆,做k-1次删除操作后堆顶元素就是我们要找的答案。参考:图解排序算法(三)之堆排序 - dreamcatcher-cx - 博客园

class Solution {
    // 使用堆排序算法
    
    public int findKthLargest(int[] nums, int k) {
        int heapSize = nums.length;
        buildMaxHeap(nums,heapSize);   
        for (int i = nums.length - 1; i >= nums.length - k + 1; --i) {
            swap(nums, 0, i);//把当前的最大值放到最右边
            --heapSize;//将堆长度减1,相当于删除了前面移除的最大值
            maxHeapify(nums, 0, heapSize);//调整最大堆
        }
        return nums[0]; //删除k个元素后的堆顶元素就是最大值
    }

    public void buildMaxHeap(int[] nums,int heapSize){
        for(int i = heapSize/2;i >= 0;i--){
            maxHeapify(nums,i,heapSize);
        }
    }

    public void maxHeapify(int[] nums,int i,int heapSize){
        int l = i*2 + 1;//左侧子节点
        int r = i*2 + 2;//右侧子节点
        int largest = i;//最大值下标

        //比较左中右三个节点,找出最大值,将最大值排在中间。
        if(l < heapSize && nums[l] > nums[largest]){
            largest = l;
        }

        if(r < heapSize && nums[r] > nums[largest]){
            largest = r;
        }

        //交换最大值的位置
        if(largest != i){
            swap(nums,i,largest);
            //继续向后递归找到每个左中右的最大值
            maxHeapify(nums,largest,heapSize);
        }
    }

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

题源:力扣

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
力扣礼盒的最大天密度问可以转化为一个函数 f(x) 的形式,其中 f(x) 表示 "礼盒中最大天数不超过 x" 这个条件下的天密度。 具体地,对于一个给定的天数 x,我们可以使用贪心算法来判断在不超过 x 的情况下,最多能拿到多少天的礼盒。假设当前已经拿到了 k 个礼盒,其天数分别为 d1, d2, ..., dk,且满足 d1 <= d2 <= ... <= dk。此时,我们可以从剩余的礼盒中选择一个最小的天数大于 dk 的礼盒,加入到已拿到的礼盒中,直到不能再加入礼盒为止。这个贪心算法的时间复杂度是 O(nlogn),其中 n 是礼盒的数量。 对于一个给定的天数 x,如果能拿到的最多天数不超过 x,则 f(x) 为 true,否则 f(x) 为 false。这个函数的曲线是一个阶梯状的函数,如下图所示: ``` | | | | | | | | | | |___|___|___|___ x1 x2 x3 x4 ``` 其中,每个竖直的线段表示一个礼盒,x1、x2、x3、x4 分别表示四个礼盒的最大天数,每个水平的线段表示函数值为 true 的区间。例如,当 x 取值在 [x3, x4] 区间内时,f(x) 的值都为 true,因为在不超过 x3 的情况下,最多能拿到的天数为 3+4+4=11,不超过 x4 的限制。 我们要到的最大的天密度,就是最后一个函数值为 true 的点所对应的 x 值,即 x4。这个问可以通过二分查法解决,每次取中间值,判断中间值是否满足条件,然后不断缩小搜索区间,最终最大的 x 值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值