当前位置:网站首页>leetcode:215. 数组中的第K个最大元素
leetcode:215. 数组中的第K个最大元素
2022-04-22 14:08:00 【OceanStar的学习笔记】
题目来源
题目描述

题目解析
调用库函数
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
std::sort(nums.begin(), nums.end());
return nums[nums.size() - k];
}
};
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
return sort(nums.begin(), nums.end(), greater<int>()), nums[k - 1];
}
};
快速排序
这道题真正要考察的应该是快速排序
class Solution {
static void show(vector<int>& array, int curr, int pos, std::string dot, bool need = true){
for(auto i : array){
std::cout << i << "\t";
}
std::cout << "\n";
if(need){
int len = array.size();
for (int j = 0; j < len; ++j) {
if(j == curr){
std::cout << "^" << "\t";
}else if(j == pos){
std::cout << dot << "\t";
}else{
std::cout << " " << "\t";
}
}
std::cout << "\n";
}
}
// 5, 4, 6
static std::tuple<int, int> helan_image(vector<int>& array, int start, int end){
int less = start - 1; // 小于
int more = end + 1;
int pivot = array[start]; // 取第一个元素为基准元素
int curr = start; // 当前正在遍历元素的下标
while (curr <= end){
if(array[curr] < pivot){
show(array, curr, less + 1, "|");
std::swap( array[curr++], array[++less]);
show(array, curr, less, "|", false);
}else if(array[curr] > pivot){
show(array, curr, more - 1, "!");
std::swap( array[curr], array[--more]);
show(array, curr, more, "!", false);
}else{
show(array, curr, curr , "^");
++curr;
show(array, curr, curr , "^", false);
}
}
return std::make_tuple(less + 1, more - 1);
}
static std::tuple<int, int> helan(vector<int>& array, int start, int end){
int less = start - 1; // 小于
int more = end + 1;
int pivot = array[start]; // 取第一个元素为基准元素
int curr = start; // 当前正在遍历元素的下标
while (curr < more){
if(array[curr] < pivot){
std::swap( array[curr++], array[++less]);
}else if(array[curr] > pivot){
std::swap( array[curr], array[--more]);
}else{
++curr;
}
}
return std::make_tuple(less + 1, more - 1);
}
void quickSort(vector<int>& array, int start, int end){
if(start >= end){
return;
}
std::tuple<int, int> mids = helan(array, start, end);
quickSort(array, start, std::get<0>(mids) - 1);
quickSort(array, std::get<1>(mids) + 1 , end);
}
public:
int findKthLargest(vector<int>& nums, int k) {
quickSort(nums, 0, (int)nums.size() - 1);
return nums[nums.size() - k];
}
};

使用优先队列
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
// 大根堆
std::priority_queue<int> q(nums.begin(), nums.end());
for (int i = 0; i < k - 1; ++i) {
q.pop();
}
return q.top();
}
};
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
// 维护一个小顶堆
std::priority_queue<int, std::vector<int>, std::greater<int>> q;
int len = nums.size();
for (int i = 0; i < len; ++i) {
q.push(nums[i]);
if(q.size() > k){
q.pop();
}
}
return q.top();
}
};
当然我们也可以换成 multiset 来做
有限队列本质上是一个堆排序
- 按输入创建一个大小为k的最小堆(vector a(k))
- 把数组的前k个数字插入堆中(带比较的插入)
- 遍历数组剩余数字,大小合适的就插入堆中(排序过程)
- 堆顶元素即为所求的第k大,前k大中的最小者
复杂度:
- 时间复杂度O(nlogk),遍历n个元素,只给k个排序
P.S. - 求最大的k个,用最小堆——要插入堆中只需要比堆中最小元素大即可
- 求最小的k个,用最大堆——要比堆中最大的小才能插入
版权声明
本文为[OceanStar的学习笔记]所创,转载请带上原文链接,感谢
https://blog.csdn.net/zhizhengguan/article/details/124339522
边栏推荐
- LeetCode-3 无重复字符的最长子串
- P2B论文复现——点云学习记录
- uniapp转微信小程序报错Cannot read property ‘forceUpdate‘ of undefined - 微信开发者工具报错
- Eight strange facts about semiconductors
- Mathorcup ideas sharing in 2022
- Redis相比memcached
- 【终于等到你】微信转发语音的方法 - 语音信息转发
- 关于QPS、TPS、并发用户数及吞吐量浅谈
- What does log desensitization mean? Why do you do log desensitization?
- redis连接工具无法连上docker中redis
猜你喜欢

那些年我们一起优化的SQL

Thoughts on dealing with high concurrency problems

Recall, this year (Huashi 918 blood and tears paste)

Imeta: integrating macroomics to re understand life and Environmental Sciences

Leetcode-386 dictionary order

FastDFS 安装和配置

產業園區數字化運營管理之“精准招商”篇

osgEarth配置地图资源

Solution to the blank page of small and medium-sized programs running from uniapp to wechat developer tool

阻塞队列-
随机推荐
Super user of oceanbase - system tenant sys
Iclr2022 outstanding Thesis Award was released, Tsinghua University and National People's Congress won awards, and Zhejiang University nominated
机器越“智能”,数据标注员越容易被淘汰?丨曼孚科技
2D转换(移动:translate,旋转:rotate,缩放:scale,2D转换综合写法)
Operation of 2022 chemical automation control instrument examination practice question simulation examination platform
LeetCode-819 最常见的单词
How to use openfeign to call the third-party interface
阻塞队列-
LeetCode——字符的最短距离
定时器--
Getting started with BCC
那些年我们一起优化的SQL
Genesis creative comics [stable pass]
An error is reported when reading the attribute value of the object through the variable storage key value in TS (TS: 7053)
二月份,我靠这一份PDF文档面试BAT,没想到竟然收到了5个offer
BCC-stackcount
2D conversion (move: translate, rotate: rotate, scale: scale, 2D conversion synthesis)
华为云媒体査勇:华为云在视频AI转码领域的技术实践
Mysql数据库转存sql文件
产业园区数字化运营管理之“精准招商”篇