当前位置:网站首页>leetcode: the Kth largest element in the array
leetcode: the Kth largest element in the array
2022-08-09 22:46:00 【Blade 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
这道题有多种解法
思路一:
Sort this array first,然后返回第kLarger elements can be subscripted.
//第一种写法
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];
}
};
思路二:
Use priority queues,Sort the elements of the array into a priority queue,默认为大堆,然后进行 k - 1次的 pop Falling head position,最后第 k A big number is in the opposite position!
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
//Put the data in the array into the priority queue first,默认为大堆
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)
So when this array is big,May cause insufficient memory,So you can look at the optimal liberation below.
思路三(最优解法):
Different from the second idea,This time we use priority queue storage k 个数,而且是按small heap storage!
Then let the remaining elements in the array be compared with the head in turn,If it is bigger than the opponent,则入堆,反之则跳过,依次循环,直到数组遍历完成.
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
//创建kA small heap of space
priority_queue<int, vector<int>, greater<int>> p(nums.begin(), nums.begin() + k);
//Put the data larger than the top of the heap into the heap each time
for(size_t i = k; i < nums.size(); ++i)
{
if(nums[i] > p.top())
{
p.pop();
p.push(nums[i]);
}
}
return p.top();
}
};
This solution isKWhen it is very large, the time complexity is similar to that of the second idea:*O(K + (N - K)logK)
But the optimization of space complexity is very large:O(K)
边栏推荐
- matlab 神经网络 ANN 分类
- Definition and Basic Operations of Linear Tables
- Access control knowledge
- Simulation of Water Temperature Control System Based on Fuzzy PID Controller
- 什么是源文件?
- C语言中的文件是什么?
- LeetCode Daily Question (321. Create Maximum Number)
- 自监督学习 —— MoCo v2
- URL Protocol 网页打开应用程序
- PMP每日一练 | 考试不迷路-8.8(包含敏捷+多选)
猜你喜欢
windos安装Mysql8.0,及解决重新登录异常问题 ERROR 1045 (28000)
【图文并茂】如何进行Win7系统的重装
Install Mysql8.0 on windos, and solve the problem of re-login exception ERROR 1045 (28000)
Word怎么制作双面席卡?使用Word制作双面席卡方法
Prometheus Operator 自定义监控添加redis explorer
How to fix Windows 11 not finding files
字节二面问的MySQL,差点没答好
URL Protocol web page to open the application
source install/setup.bash时出现错误
leetcode 二叉树的公共近祖先
随机推荐
场效应管Mosfet之雷卯Leiditech对应英飞凌Infineon
一种基于连接和安全熵的网络空间整体安全认识和方法
Error when source install/setup.bash
Don't use array.length-1 to get the last item of the array
Word第一页不要页眉怎么设置?设置Word首页不要页眉方法教程
leetcode 二叉树的公共近祖先
Excel如何打出正负号?Excel打出正负号的方法
cadence中复制一部分PCB到另一个PCB中去
什么是源文件?
Acrel5000web能耗系统在某学院的应用-Susie 周
基于Docker构建MySQL主从复制数据库
How to deal with keys when Redis is large?
visual studio 2022调试技巧介绍
DSPE-PEG-PDP,DSPE-PEG-OPSS,磷脂-聚乙二醇-巯基吡啶可减少肽的免疫原性
hdu 3341 Lost's revenge(dp+Ac自动机)
[corctf 2022] 部分
How are data integration APIs key to enterprise digital transformation?
[corctf 2022] section
LeetCode Daily Question (321. Create Maximum Number)
Week 8 Deep learning for object detection