最小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个数不断插入最大堆,对于之 ...
随机推荐
- JavaWeb(一)之细说Servlet
前言 其实javaWeb的知识早就学过了,可是因为现在在搞大数据开发,所以web的知识都忘记了.准备开始慢慢的把Web的知识一点一点的回忆起来,多学一点没有关系,就怕到时候要用的话,什么都不会了. 一 ...
- Variational Bayes
一.前言 变分贝叶斯方法最早由Matthew J.Beal在他的博士论文<Variational Algorithms for Approximate Bayesian Inference> ...
- 【框架学习与探究之宿主服务--Topshelf】
前言 此文欢迎转载,原始链接地址:http://www.cnblogs.com/DjlNet/p/7603819.html 正文 原先也偶然见过这个关键词,当时只是有个大致了解貌似和WinServic ...
- 从头编写 asp.net core 2.0 web api 基础框架 (2)
上一篇是: http://www.cnblogs.com/cgzl/p/7637250.html Github源码地址是: https://github.com/solenovex/Building- ...
- epoll模型的使用
1. 创建epoll句柄 int epfd = epoll_create(int size); 该函数生成一个epoll专用的文件描述符.它其实是在内核申请一空间,用来存放你想关注的socket fd ...
- 高效实用的.NET开源项目
似乎...很久很久没有写博客了,一直都想写两篇,但是却没有时间写.感觉最近有很多事情需要处理,一直都是疲于奔命,一直到最近才变得有些时间学习和充电.最近没有事情都会看一些博客和开源项目,发现介绍开源项 ...
- Mybatis了解(配置)
Mybatis是一个基于jdbc映射框架.它跟hibernate一样都是对数据库进行操作的.Mybatis 它是通过配置xml或者是注解来进行映射的配置,最后实现操作接口与pojo来操作数据库. 因此 ...
- TypeScript中的怪语法
TypeScript中的怪语法 如何处理undefined 和 null undefined的含义是:一个变量没有初始化. null的含义是:一个变量的值是空. undefined 和 null 的最 ...
- 见到过的MOS管的一些参数
选择MOS管的时候需要注意的几个参数1,VDSS(击穿电压):此电压要选择合适,一般是加入的电压值的峰值的两倍.2,VGS(th)(开启电压):3,RDS(on)(漏源电阻):这个值要尽可能的小,因为 ...
- uva11584
将课本上所述方法实现即可,代码如下: /* * Author: Bingo * Created Time: 2015/1/25 23:49:49 * File Name: uva11584.cpp * ...