一题多解(五) —— topK(数组中第 k 大/小的数)
根据对称性,第 k 大和第 k 小,在实现上,是一致的,我们就以第 k 小为例,进行说明:
法 1
直接排序(sort(A, A+N)
),当使用一般时间复杂度的排序算法时,其时间复杂度为 O(N2)
法 2
先将 k 个元素读入一个数组并将其排序,sort(A, A+k)
,则这些元素的最大值在第 k 个位置上。我们一个一个处理剩余的元素,当一个元素开始被处理时,它先于数组中第 k 个元素比较,
- 如果该元素大,不做任何处理,遍历下一个元素;
- 如果该元素小,则将该元素插入在前面合适的位置;
代码逻辑可参考 插入排序(insertion sort)
此时的时间复杂度为:O(k2+(N−k)⋅k)=O(N⋅k)
法 3:借助数据结构
优先队列,DeleteMin 执行 k-1 次,
法 4:借助快排的 partition 函数
根据 partition 函数返回值与 k 的关系,然后继续进行 partition:
int partition(int*A, int N, int s, int e) {
if (!A || N <= 0 || s < 0 || e >= N)
throw exception("");
int toSwap = s - 1;
// 以最后一个元素作为 pivot,然后进行
for (int i = s; i < e; ++i) {
if (A[i] < A[e]) {
++toSwap;
if (toSwap != i) {
swap(A[toSwap], A[i]);
}
}
}
++toSwap;
swap(A[toSwap], A[e]);
}
int topK (int*A, int N, int k) {
int p = partition(A, N, 0, N-1);
while (p != k-1) {
p < k-1 ? p = partition(A, N, p+1, N-1) : p = partition(A, N, 0, p-1);
}
return A[p];
}
一题多解(五) —— topK(数组中第 k 大/小的数)的更多相关文章
- TopK问题,数组中第K大(小)个元素问题总结
问题描述: 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 面试中常考的问题之一,同时这道题由于解法众多,也是考察时间复杂 ...
- [经典算法题]寻找数组中第K大的数的方法总结
[经典算法题]寻找数组中第K大的数的方法总结 责任编辑:admin 日期:2012-11-26 字体:[大 中 小] 打印复制链接我要评论 今天看算法分析是,看到一个这样的问题,就是在一堆数据 ...
- 前端算法题:找出数组中第k大的数字出现多少次
题目:给定一个一维数组,如[1,2,4,4,3,5],找出数组中第k大的数字出现多少次. 例如:第2大的数是4,出现2次,最后输出 4,2 function getNum(arr, k){ // 数组 ...
- [LeetCode] Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- [LeetCode] 215. Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- 查找数组中第k大的数
问题: 查找出一给定数组中第k大的数.例如[3,2,7,1,8,9,6,5,4],第1大的数是9,第2大的数是8-- 思考:1. 直接从大到小排序,排好序后,第k大的数就是arr[k-1]. 2. ...
- 寻找数组中第K大的数
给定一个数组A,要求找到数组A中第K大的数字.对于这个问题,解决方案有不少,此处我只给出三种: 方法1: 对数组A进行排序,然后遍历一遍就可以找到第K大的数字.该方法的时间复杂度为O(N*logN) ...
- [leetcode]215. Kth Largest Element in an Array 数组中第k大的元素
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- 无序数组中第K大的数
1. 排序法 时间复杂度 O(nlogn) 2. 使用一个大小为K的数组arr保存前K个最大的元素 遍历原数组,遇到大于arr最小值的元素时候,使用插入排序方法,插入这个元素 时间复杂度,遍历是 O( ...
随机推荐
- Css 显示删除条目效果
样式设置
- Flask项目之手机端租房网站的实战开发(五)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...
- VC++ 6.0 BUG BUG BUG BUG BUG
http://blog.163.com/amao831@126/blog/#m=0 我经常在的VC++6.0中 定义某个类的对象时 再用.访问或者->访问时不自动弹出他的成员函数或者成员变量 最 ...
- LeetCode_Construct Binary Tree from Preorder and Inorder Traversal
一.题目 Construct Binary Tree from Preorder and Inorder Traversal My Submissions Given preorder and ino ...
- Hadoop3.0配置
1.core-site.xml <configuration> <property> <name>fs.default.namenode</name> ...
- Surging 微服务框架使用入门
原文:Surging 微服务框架使用入门 前言 本文非 Surging 官方教程,只是自己学习的总结.如有哪里不对,还望指正. 我对 surging 的看法 我目前所在的公司采用架构就是类似与Sur ...
- [React Intl] Format Date and Time Using react-intl FormattedDate and FormattedTime
Using the react-intl FormattedDate and FormattedTime components, we’ll render a JavaScript Date into ...
- Scala在挖财的应用实践--转载
原文地址:http://www.infoq.com/cn/articles/scala-architecture-wacai 编者按:本文是根据ArchSummit大会上挖财资深架构师王宏江的演讲&l ...
- YUM查询软件信息
我们常会碰到这样的情况,想要安装一个软件,只知道它和某方面有关,但又不能确切知道它的名字.这时yum的查询功能就起作用了.你可以用yum search keyword这样的命令来进行搜索,比如我们要则 ...
- 交换排序(java)
package exchange_sort; import java.util.Random; /*各类交换排序 * ------数据存储范围1~s.length-1 ------ *主要包含 ...