当前位置:网站首页>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;
}
};
边栏推荐
- Handwriting a blogging platform ~ Day 3
- From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.
- 力扣(LeetCode)213. 打家劫舍 II(2022.08.01)
- 2022-08-01 Install mysql monitoring tool phhMyAdmin
- 【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
- 使用DBeaver进行mysql数据备份与恢复
- oracle query scan full table and walk index
- Centos7 安装postgresql并开启远程访问
- "NetEase Internship" Weekly Diary (2)
- Redis 底层的数据结构
猜你喜欢

【LeetCode每日一题】——704.二分查找

Redis Subscription and Redis Stream

How to adjust the cross cursor too small, CAD dream drawing calculation skills
![[LeetCode Daily Question]——654. The largest binary tree](/img/05/0af1c6dc0085e253c0758c8da63e52.png)
[LeetCode Daily Question]——654. The largest binary tree

Golang分布式应用之定时任务

Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒

个人博客系统项目测试

Data transfer at the data link layer

Chopper webshell feature analysis

2022河南青训联赛第(三)场
随机推荐
Rasa 3 x learning series - Rasa - 4873 dispatcher Issues. Utter_message study notes
leetcode/字符串中的变位词-s1字符串的某个排列是s2的子串
永磁同步电机36问(二)——机械量与电物理量如何转化?
Nanoprobes纳米探针丨Nanogold偶联物的特点和应用
"NetEase Internship" Weekly Diary (3)
Project Background Technology Express
[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline
Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products
垃圾回收器CMS和G1
nacos startup error, the database has been configured, stand-alone startup
Handwriting a blogging platform ~ the first day
优炫数据库导库导错了能恢复吗?
Effects of Scraping and Aggregation
Rasa 3.x 学习系列- Rasa - Issues 4873 dispatcher.utter_message 学习笔记
The failure to create a role in Dahua Westward Journey has been solved
How engineers treat open source
2023年起,这些地区软考成绩低于45分也能拿证
接口测试神器Apifox究竟有多香?
BI-SQL丨WHILE
CodeTon Round 2 D. Magical Array 规律