当前位置:网站首页>53. 最小的k个数
53. 最小的k个数
2022-08-02 02:20:00 【Hunter_Kevin】
53. 最小的k个数
题目
输入 n 个整数,找出其中最小的 k 个数。
注意:
输出数组内元素请按从小到大顺序排序;
数据范围
1≤k≤n≤1000
样例
输入:[1,2,3,4,5,6,7,8] , k=4
输出:[1,2,3,4]
思路一:小根堆,输出前k个堆顶元素
0
1 2
3 4 5 6
7 8
当堆数组长度n为9时,则从下标为3开始往上down操作,即从9/2 - 1 == 3
down()中对应的左孩子节点下标为2*u+1, 右孩子节点下标为2*u+2
class Solution {
public:
void down(vector<int>& input, int u, int len){
int t = u;
//此处是数组的下标,限制不越界即可
if(2*u+1 <= len && input[2*u+1] < input[t]) t = 2*u+1;
if(2*u+2 <= len && input[2*u+2] < input[t])t = 2*u+2;
if(t != u){
swap(input[u],input[t]);
down(input,t,len);//递归对交换的位置的元素执行down()
}
}
vector<int> getLeastNumbers_Solution(vector<int> input, int k) {
//进行堆排序
int len = input.size()-1;
//数组下标从0开始,如果数组长度为n,则从数组下标为n/2 -1的地方开始往上执行down()
for(int i = input.size()/2 - 1; i>=0; i--) down(input, i, len);
vector<int> res;
while(k--){
res.push_back(input[0]);//添加到vector中
input[0] = input[len];//把堆中的末尾元素放到堆顶
len--;//存储堆的数组长度减少
down(input,0,len);//对堆顶执行down
}
return res;
}
};
思路二:维护一个k个数的大根堆
C++中的大根堆是优先队列
priority_queue<>
class Solution {
public:
vector<int> getLeastNumbers_Solution(vector<int> input, int k) {
//遍历数组,维护一个堆个数为k的大根堆,最后将大根堆放到vector中,此时序列从大到小,再反转数组即可
priority_queue<int> heap;
// 遍历给定的数组
for(auto num : input){
heap.push(num);
if(heap.size() > k) heap.pop();
}
vector<int> res;
// 把维护的大根堆中的k个数放到数组中
while(heap.size()){
res.push_back(heap.top());
heap.pop();
}
// 反转数组
reverse(res.begin(),res.end());
return res;
}
};
边栏推荐
- Rasa 3 x learning series - Rasa - 4873 dispatcher Issues. Utter_message study notes
- Remember a pit for gorm initialization
- Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
- Ask God to answer, how should this kind of sql be written?
- The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
- MySQL - CRUD operations
- Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
- 记一次gorm事务及调试解决mysql死锁
- 2022河南青训联赛第(三)场
- 字典常用方法
猜你喜欢

垃圾回收器CMS和G1

Constructor instance method inheritance of typescript37-class (extends)

A good book for newcomers to the workplace

永磁同步电机36问(二)——机械量与电物理量如何转化?

Personal blog system project test

"NetEase Internship" Weekly Diary (1)

使用docker安装mysql

永磁同步电机36问(三)——SVPWM代码实现

Use baidu EasyDL implement factory workers smoking behavior recognition

Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
随机推荐
NIO‘s Sword(牛客多校赛)
BI-SQL丨WHILE
十字光标太小怎么调节、CAD梦想画图算量技巧
The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string
AI target segmentation capability for fast video cutout without green screen
【LeetCode每日一题】——654.最大二叉树
Data transfer at the data link layer
Simple example of libcurl accessing url saved as file
AI目标分割能力,无需绿幕即可实现快速视频抠图
LeetCode刷题日记:LCP 03.机器人大冒险
2022-08-01 反思
2022-08-01 mysql/stoonedb慢SQL-Q18分析
leetcode/字符串中的变位词-s1字符串的某个排列是s2的子串
【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
局部敏感哈希:如何在常数时间内搜索Embedding最近邻
Handwriting a blogging platform ~ Day 3
LeetCode brushing diary: 53, the largest sub-array and
Chopper webshell feature analysis
列表常用方法