当前位置:网站首页>leetcode:215无序数组中找第k大的元素
leetcode:215无序数组中找第k大的元素
2022-08-04 14:31:00 【OceanStar的学习笔记】
题目来源
题目描述
题目解析
快排
原数组是要求第k
大的问题可以转换为求第size - k + 1
小的问题
思路
- 无序数组中随机选择一个数V,然后对V对整个数组做荷兰国旗问题。此后,<V的在左边,=V的在中间,>V的在右边
- 如果命中就停,如果没有命中,然后根据实际选择左侧或者右侧回到第一步
- 时间复杂度O(N)
实现
class Solution {
std::vector<int> partition(std::vector<int>& arr, int L, int R, int prio){
int less = L - 1, more = R + 1, curr = L;
while (curr < more){
if(arr[curr] < prio){
std::swap(arr[++less], arr[curr++]);
}else if(arr[curr] > prio){
std::swap(arr[curr], arr[--more]);
}else{
curr++;
}
}
return {
less + 1, more - 1};
}
// arr[L..R] 范围上,如果排序的话(不是真的去排序),找位于index的数
int process(std::vector<int> arr, int L, int R, int index){
if(L == R){
//
return arr[L];
}
int pivot = arr[rand() % (R - L + 1) + L];
auto range = partition(arr, L, R, pivot);
if (index >= range[0] && index <= range[1]) {
return arr[index];
} else if (index < range[0]) {
return process(arr, L, range[0] - 1, index);
} else {
return process(arr, range[1] + 1, R, index);
}
}
public:
int findKthLargest(vector<int> &nums, int k){
srand(time(0));
int result = 0;
int numsSize = int(nums.size());
if (numsSize == 0 || k > numsSize)
{
return 0;
}
//寻找第kMIN小的数
int kMin = numsSize - k + 1; //第k小
result = process(nums, 0, numsSize - 1, kMin - 1);
return result;
}
};
优先级队列
思路
- 唯一一个长度为k的小根堆,保持队列中的元素时钟为k个
- 遍历数组nums,入队一个元素之后,立即弹出堆顶最小的元素
- 遍历完成之后,堆顶的元素就是第k大的数字
ps:求第k大的数用小根堆,求第k小的数用大根堆
时间复杂度O(N*logK)
什么时候用?
在数据量很大的时候,可以实现「在线算法」,不用一下子把所有数据读入内存。
实现
class Solution {
public:
int findKthLargest(vector<int> &arr, int k){
std::priority_queue<int , std::vector<int>, std::greater<>> minheap;
for (int i = 0; i < k; ++i) {
minheap.push(arr[i]);
}
for (int i = k; i < arr.size(); ++i) {
if(arr[i] < minheap.top()){
minheap.pop();
minheap.push(arr[i]);
}
}
return minheap.top();
}
};
边栏推荐
- 南瓜科学产品升级 开启益智探索新篇章
- 华为手机切换屏幕效果_华为p40页面切换效果怎么换
- 郑轻新生校赛和中工选拔赛题解
- 《中国综合算力指数》《中国算力白皮书》《中国存力白皮书》《中国运力白皮书》在首届算力大会上重磅发出
- Why does the decimal point appear when I press the space bar in word 2003?
- Theory 1: Deep Learning - Detailed Explanation of the LetNet Model
- js深拷贝和浅拷贝具体使用区别_es6深拷贝和浅拷贝
- 【北亚数据恢复】IBM System Storage存储lvm信息丢失数据恢复方案
- MySQL【触发器】
- [Opportunity Enlightenment-60]: "Soldiers, Stupid Ways"-1- Opening: "Death" and "Life" are the way of heaven
猜你喜欢
随机推荐
Almost all known protein structures in the world are open sourced by DeepMind
AOSP内置APP特许权限白名单
B. Construct a simple sequence (greedy)
[Beiya data recovery] IBM System Storage storage lvm information lost data recovery solution
爬虫——selenium基本使用、无界面浏览器、selenium的其他用法、selenium的cookie、爬虫案例
Makefile 语法及使用笔记
【历史上的今天】8 月 4 日:第一位图灵奖女性得主;NVIDIA 收购 MediaQ;首届网络安全挑战大赛完成
Phasecraft连下两城,助力英国量子技术商业化加速!
杭电校赛(逆袭指数)
metaRTC5.0新版本支持mbedtls(PolarSSL)
特殊品种的二次开户验资金额
集合划分差最小问题(01背包)
How to write SQL statements: the usage of Update, Case, and Select together
【剑指offer33】二叉搜索树的后序遍历序列
vim 常用操作命令
Lixia Action | Kyushu Yunzhang Jinnan: Open source is not a movement for a few people, popularization is the source
用于X射线聚焦的复合折射透镜
token 过期后,如何自动续期?
How to install postgresql and configure remote access in ubuntu environment
郑轻新生校赛和中工选拔赛题解