当前位置:网站首页>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)
边栏推荐
- Win11搜索不到文件的解决方法
- Two methods of implementing inverted strings in C language
- Next second data: the transformation of the modern data stack brought about by the integration of lake and warehouse has begun
- 安科瑞无线物联网智能电表ADW300指导性技术要求-Susie 周
- 6 g underwater channel modeling were summarized based on optical communication
- LeetCode每日一题(321. Create Maximum Number)
- What are the benefits of enterprise data integration?How do different industries solve the problem of data access?
- 2.3 监督学习-2
- 【深度学习】pix2pix GAN理论及代码实现
- fixed investment fund
猜你喜欢
3D感知(二):单目3D物体检测
Acrel5000web能耗系统在某学院的应用-Susie 周
Unity2D_线框材质
leetcode二叉搜索树与双向链表
tki-tree 树组件控制默认展开第几层数据
URL Protocol web page to open the application
Definition and Basic Operations of Linear Tables
基于模糊PID控制器的水温控制系统仿真
DSPE-PEG-PDP, DSPE-PEG-OPSS, phospholipid-polyethylene glycol-mercaptopyridine reduce the immunogenicity of peptides
buuctf(探险2)
随机推荐
2.2 监督学习-1
【IoT毕设】STM32与机智云自助开发平台的宠物智能喂养系统
基于模糊PID控制器的水温控制系统仿真
Laravel之队列「建议收藏」
力扣 899. 有序队列
FS4066耐高压1到4节内置MOS的锂电池充电管理芯片
Oracle 字段自增
How are data integration APIs key to enterprise digital transformation?
小满nestjs(第三章 前置知识装饰器)
LoRa Basics无线通信技术和应用案例详解
39. 组合总和 && 40. 组合总和2 && 216. 组合总和3
SqlServer 2016 备份和还原
Definition and Basic Operations of Sequence Tables
php删除字符串的空格
Overview of Security Analysis Technology for Smart Home Devices
【kali-密码攻击】(5.1.1)密码在线破解:Hydra(图形界面)
UE4_定序器控制蓝图对象
axi4c
Toronto Research Chemicals盐酸乙环胺应用说明
力扣15-三数之和——HashSet&双指针法