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.

The same with Lintcode: Kth largest element

快速选择 Quick Select

复杂度

时间 Avg O(N) Worst O(N^2) 空间 O(1)

One thing to notice is 23-24 line, still find kth smallest element, do not need to decrease from k because the indexing(编号) does not change

 1 public class Solution {
 2     public int findKthLargest(int[] nums, int k) {
 3         int len = nums.length;
 4         return findKthSmallest(nums, 0, len-1, len-k+1);
 5     }
 6
 7     public int findKthSmallest(int[] nums, int start, int end, int k) {
 8         int l = start;
 9         int r = end;
10         int pivot = end;
11         while (l < r) {
12             while (l<r && nums[l] < nums[pivot]) {
13                 l++;
14             }
15             while (l<r && nums[r] >= nums[pivot]) {
16                 r--;
17             }
18             if (l == r) break;
19             swap(nums, l, r);
20         }
21         swap(nums, l, pivot);
22         if (l+1 == k) return nums[l];
23         else if (l+1 < k) return findKthSmallest(nums, l+1, end, k);
24         else return findKthSmallest(nums, start, l-1, k);
25     }
26
27     public void swap(int[] nums, int l, int r) {
28         int temp = nums[l];
29         nums[l] = nums[r];
30         nums[r] = temp;
31     }
32 }

优先队列

复杂度

时间 O(NlogK) 空间 O(K)

思路

遍历数组时将数字加入优先队列(堆),一旦堆的大小大于k就将堆顶元素去除,确保堆的大小为k。遍历完后堆顶就是返回值。

 1 public class Solution {
 2     public int findKthLargest(int[] nums, int k) {
 3         PriorityQueue<Integer> p = new PriorityQueue<Integer>();
 4         for(int i = 0 ; i < nums.length; i++){
 5             p.add(nums[i]);
 6             if(p.size()>k) p.poll();
 7         }
 8         return p.poll();
 9     }
10 }

用最大堆也可以,把所有元素加进去后,poll K次

转载于:https://www.cnblogs.com/EdwardLiu/p/5052893.html

Leetcode: Kth Largest Element in an Array相关推荐

  1. LeetCode——Kth Largest Element in an Array

    LeetCode--Kth Largest Element in an Array Question Find the kth largest element in an unsorted array ...

  2. LeetCode Kth Largest Element in an Array(小根堆)

    问题:求数组中第k在的数 思路:使用小根堆,当堆中数量小于k时,将数据放入堆中,如果当前数大于堆顶的数,则弹出,将当前数放入堆中 具体代码参考: https://github.com/wuli2496 ...

  3. 剑指offer 最小的k个数 leetcode 215. Kth Largest Element in an Array

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

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

    传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...

  5. 力扣215:数组中的第K个最大元素 (leetcode 215:Kth Largest Element In An Array)

    题目链接:https://leetcode.cn/problems/kth-largest-element-in-an-array 目录: 一.题目描述 1.中文 2.英文 二.解决方法 1.直接排序 ...

  6. [swift] 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 so ...

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

  8. 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 so ...

  9. [leedcode 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 so ...

最新文章

  1. ubuntu系统php环境变量设置,Ubuntu系统环境变量详解
  2. GraphQL 01--- GraphQL 介绍及资源总结
  3. Javascript绝句欣赏
  4. Python多进程之构建进程池
  5. 【Qt】QModbusExceptionResponse类
  6. 《MySQL 8.0.22执行器源码分析(3.2)关于HashJoinIterator》
  7. 算法五——字符串匹配(中)
  8. do matlab的 while循环_除了Matlab,基本上所有的语言都有do-while语句吧?的相关推荐_ChinaUnix论坛...
  9. NutzWk 5.0.x 微服务分布式版本开发及部署说明
  10. mysql怎么导入代码_mysql导入数据步骤mysqlimport
  11. html span向上对齐,css也能让span靠左布局
  12. 资产证券化为什么需要区块链技术?专访趣链科技揭开“区块链+ABS”迷雾
  13. iOS调用手机振动和铃声
  14. python人文社科研究_人文社科论文写作数据分析利器|SPSS+Stata+Endnote+Python
  15. 使用计算机要遵循哪些规则,中国大学MOOC:\\\在计算机网络的定义中,把众多计算机有机连接起来要遵循规定的约定和规则,称之为( )。\\\;...
  16. 例说游戏角色设计与角色文化内涵的关系
  17. 图片编辑软件_pinta在Linux下安装
  18. 自然语言处理NLP星空智能对话机器人系列:深入理解Transformer自然语言处理 Noisy relationships
  19. 20-Django REST framework-Serializer序列化器
  20. 企业家论坛谋定转型 商协社团·万祥军:全国工商联促广东发展

热门文章

  1. androidstudio带pom的上传到jcenter_输送机@网带输送机@304网带输送机@304不锈钢网带输送机@输送机网带厂家定制...
  2. python将图像转换为8位单通道_使用Python将图片转换为单通道黑白图片
  3. 计算机文档设置,电脑这样设置快速的共享文件、分享文档!
  4. 汇编语言中变量的声明
  5. Linux网络故障排查命令(ifconfig、ping、telnet、netstat、lsof、nc、curl、tcpdump)
  6. python掷骰子_用于掷骰子的Python程序(2人骰子游戏)
  7. 爬虫项目(四)---采集从01月22日以来全国各省疫情数据
  8. Python---实验八
  9. Python---二分法查找
  10. linux内核设计与实现---从内核出发