当前位置:网站首页>leetcode:数组中的第K个最大元素
leetcode:数组中的第K个最大元素
2022-08-09 19:34:00 【利刃Cc】
215. 数组中的第K个最大元素
难度中等1787
给定整数数组 nums
和整数 k
,请返回数组中第 **k**
个最大的元素。
请注意,你需要找的是数组排序后的第 k
个最大的元素,而不是第 k
个不同的元素。
你必须设计并实现时间复杂度为 O(n)
的算法解决此问题。
示例 1:
输入: [3,2,1,5,6,4], k = 2
输出: 5
示例 2:
输入: [3,2,3,1,2,4,5,5,6], k = 4
输出: 4
提示:
1 <= k <= nums.length <= 105
-104 <= nums[i] <= 104
这道题有多种解法
思路一:
先将这个数组进行排序,然后返回第k大的元素下标即可。
//第一种写法
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
return nums[nums.size() - k];
}
};
//第二种写法
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
sort(nums.rbegin(), nums.rend());
return nums[k - 1];
}
};
//第三种写法
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
sort(nums.begin(), nums.end(), greater<int>());
return nums[k - 1];
}
};
思路二:
运用优先级队列,将数组的元素放到优先级队列中排序,默认为大堆,然后进行 k - 1次的 pop 掉队头的位置,最后第 k 个大的数字就在对头的位置了!
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
//将数组里面的数据先放到优先级队列中,默认为大堆
priority_queue<int> p(nums.begin(), nums.end());
//将队列中前k-1个最大的元素pop掉
for(int i = 0; i < k - 1; ++i)
{
p.pop();
}
return p.top();
}
};
时间复杂度:O(K + K*logN)
时间复杂度:O(N)
所以当这个数组很大的时候,可能会导致内存不够,所以可以看下面这种最优的解放。
思路三(最优解法):
与思路二不同,这次我们用优先级队列存储 k 个数,而且是按小堆存放!
然后让数组里面剩余元素依次与对头比较,若比对头还大的话,则入堆,反之则跳过,依次循环,直到数组遍历完成。
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
//创建k个空间的小堆
priority_queue<int, vector<int>, greater<int>> p(nums.begin(), nums.begin() + k);
//将每次大于堆顶的数据入堆
for(size_t i = k; i < nums.size(); ++i)
{
if(nums[i] > p.top())
{
p.pop();
p.push(nums[i]);
}
}
return p.top();
}
};
这种解法当K很大的时候的时间复杂度与思路二差不多:*O(K + (N - K)logK)
但是对于空间复杂度的优化则非常的大:O(K)
边栏推荐
- [Graphic and textual] How to reinstall Win7 system
- Overview of Security Analysis Technology for Smart Home Devices
- 获取数组最后一项别再用array.length-1了
- 【二叉树】树的子结构
- Unity_物体自转
- 如何在WPF中设置Grid ColumnDefinitions的样式
- 奥特曼卡牌隐藏的百亿市场
- DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰二氧化硅颗粒用
- MySQL, which is asked on both sides of the byte, almost didn't answer well
- 日期及时间处理包 Carbon 在 Laravel 中的简单使用[通俗易懂]
猜你喜欢
数据分散情况的统计图-盒须图
buuctf (Adventure 2)
威纶通触摸屏制作自定义弹出窗口的具体方法(3种)
Puyuan Jingdian turned losses into profits in the first half of the year, and high-end products continued to develop!Are you optimistic about "Huawei" in the instrument industry?
渗透测试-对新型内存马webshell的研究
Prometheus Operator 自定义监控添加redis explorer
Toronto Research Chemicals单羟基舒更葡糖钠说明书
访问控制知识
Definition and Basic Operations of Sequence Tables
[Deep learning] pix2pix GAN theory and code implementation
随机推荐
Ankerui supports Ethernet communication, profibus communication embedded energy meter APM guiding technical requirements-Susie Week
hdu 3341 Lost's revenge(dp+Ac自动机)
看完这波 Android 面试题;助你斩获心中 offer
大健康产业商业供应链管理系统数字化提升产业链运作效率推动供应链标准化建设
UE4_定序器控制蓝图对象
Laravel DB批量更新的方法
2.2 监督学习-1
Toronto Research Chemicals加米霉素-d4说明书
中英文说明书丨Abbkine细胞迁移分析试剂盒
Week 8 Deep learning for object detection
阿里二面:没有 accept,能建立 TCP 连接吗?
2.3 监督学习-2
DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified silica particles
韩国网络安全体系特征与发展前景
What are the benefits of enterprise data integration?How do different industries solve the problem of data access?
SqlServer 2016 安装相关问题
【图文并茂】如何进行Win7系统的重装
gmail+mtalk配合打免费网络电话。
新起之秀 DPU,正在掀起数据中心变革!
CMake installation upgrade higher version