最小k个数
题目
输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
思考
- 方法0: 直接排序然后返回前k个,最好的时间复杂度为 O(nlog(n))
- 方法1: 快排的变种,时间复杂度 O(n),缺点:原址,需要把所有数都 load 到内存中
- 方法2: 利用最大堆作为辅助,时间复杂度 O(n*lg(k)),适用于处理数据量很大的情况。
code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution{
public:
vector<int> GetLeastNumbers_Solution(vector<int> input, int k){
vector<int> ans;
if(k > input.size()){
return ans;
}
// GetLeastNumbersPartition(input, 0, input.size()-1, k);
// for(int i=0; i<k; i++)
// ans.push_back(input[i]);
// return ans;
return GetLeastNumbersHeap(input, k);
}
// parition, average time complexity - O(n)
// note: k should less than then size of input
void GetLeastNumbersPartition(vector<int> &input, int left, int right, int k){
int pos = partition(input, left, right);
if(pos == k-1){
return;
}else if (pos < k-1){
GetLeastNumbersPartition(input, pos+1, right, k);
}else{
GetLeastNumbersPartition(input, left, pos-1, k);
}
}
int partition(vector<int> &input, int left, int right){
if(left > right)
return -1;
int pos = left-1;
for(int i=left; i<right; i++){
if(input[i] <= input[right]){
swap(input[i], input[++pos]);
}
}
swap(input[right], input[++pos]);
// input[left, pos] <= input[pos]
// input[pos+1, right] > input[pos]
return pos;
}
// heap sort, time complexity - O(nlog(k))
vector<int> GetLeastNumbersHeap(vector<int> &input, int k){
if(k > input.size() || input.empty())
return vector<int>();
vector<int> ans(input.begin(), input.begin()+k); // max heap
make_heap(ans.begin(), ans.end(), comp);
for(int i=k; i<input.size(); i++){
if(input[i] < ans.front()){ // the current value less than the maximun of heap
pop_heap(ans.begin(), ans.end(), comp);
ans.pop_back();
ans.push_back(input[i]);
push_heap(ans.begin(), ans.end(), comp);
}
}
sort(ans.begin(), ans.end());
return ans;
}
static bool comp(int a, int b){
return a<b;
}
};
int main()
{
freopen("in.txt", "r", stdin);
int k;
cin >> k;
vector<int> input;
int cur;
while(cin >> cur){
input.push_back(cur);
}
vector<int> ans = Solution().GetLeastNumbers_Solution(input, k);
for(int n : ans)
cout << n << " ";
cout << endl;
fclose(stdin);
return 0;
}
最小k个数的更多相关文章
- 求n个数中的最大或最小k个数
//求n个数中的最小k个数 public static void TestMin(int k, int n) { Random rd = new Ra ...
- nyoj 678 最小K个数之和
最小K个数之和 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 输入n个整数,输出其中最小的K个数之和.例如输入4,5,1,1,6,2,7,3,3这9个数字,当k=4 ...
- 最小K个数之和
描述 输入n个整数,输出其中最小的K个数之和.例如输入4,5,1,1,6,2,7,3,3这9个数字,当k=4,则输出最小的4个数之和为7(1,1,2,3). 输入 测试样例组数不超过10 每个测试案例 ...
- 算法试题 - 找出最小 k 个数
题目 题目:输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 解析 思路1 这一题应用堆排序算法复杂度只有O(nlog k), ...
- 【13】堆排序 最小K个数
题目 输入整数数组 arr ,找出其中最小的 k 个数.例如,输入4.5.1.6.2.7.3.8这8个数字,则最小的4个数字是1.2.3.4. 收获 优先队列实现 (n1,n2)->n2-n1是 ...
- 剑指Offer28 最小的K个数(Partition函数应用+大顶堆)
包含了Partition函数的多种用法 以及大顶堆操作 /*********************************************************************** ...
- 找出最小的k个数
•已知数组中的n个正数,找出其中最小的k个数. •例如(4.5.1.6.2.7.3.8),k=4,则最小的4个数是1,2,3,4 •要求: –高效: –分析时空效率 •扩展:能否设计出适合在海量数据中 ...
- 编程算法 - 最小的k个数 红黑树 代码(C++)
最小的k个数 红黑树 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入n个整数, 找出当中的最小k个数. 使用红黑树(multiset) ...
- 笔试算法题(03):最小第K个数 & 判定BST后序序列
出题:输入N个整数,要求输出其中最小的K个数: 分析: 快速排序和最小堆都可以解决最小(大)K个数的问题(时间复杂度为O(NlogN)):另外可以建立大小为K的最大堆,将前K个数不断插入最大堆,对于之 ...
随机推荐
- JSP入门 导出文件
1.图片校验码 <img src="captcha.jpg" /> web.xml配置 <servlet> <servlet-name& ...
- 51nod 1126 求递推序列的第N项 思路:递推模拟,求循环节。详细注释
题目: 看起来比较难,范围10^9 O(n)都过不了,但是仅仅是看起来.(虽然我WA了7次 TLE了3次,被自己蠢哭) 我们观察到 0 <= f[i] <= 6 就简单了,就像小学初中学的 ...
- D. How many trees? DP
D. How many trees? time limit per test 1 second memory limit per test 64 megabytes input standard in ...
- Google Authenticator 如何集成(U盾的实现原理相同)
Google Authenticator是一个类似U盾的二次验证工具,Google提供了它的开源客户端(https://github.com/google/google-authenticator)里 ...
- C语言程序设计第一作业
C语言程序设计第一作业 实验总结 (一) 1.题目:输入圆的半径,求圆周长和面积 2.流程图: 3.测试数据及运行结果: 4.实验分析: 问题1: 出现了错误 原因:是在赋值那写反了 解决方法:应该是 ...
- 有人提了一个问题:一定要RESTful吗?
写在前面的话 这个问题看起来就显得有些萌,或者说类似的问题都有些不靠谱,世上哪有那么多一定的事情,做开发都不一定做多久呢,所以说如果你有这个疑问的话是真真有点儿不着调,不过可能也就是随口一问吧,没有深 ...
- MyServer
//一.设置一个8089端口的本地IP服务器 1 package myserver; import java.io.IOException; import java.net.ServerSocket; ...
- Linux-问题集锦(1)
一. 某用户只读特定文件夹 只读目录 : /home/www/yqz/logs 1. 创建用户 useradd ReadOnly passwd ReadOnly 2. ...
- Android Annotations(2)
AndroidAnnotations是一个开源框架,旨在加快Android开发的效率.通过使用它开放出来的注解api,你几乎可以使用在任何地方, 大大的减少了无关痛痒的代码量,让开发者能够抽身其外,有 ...
- UVa816,Ordering Tasks,WA
#include <iostream> #include <cstdio> #include <string> #include <cstring> # ...