当前位置:网站首页>Leetcode: 215 disorderly to find the first big k element in the array
Leetcode: 215 disorderly to find the first big k element in the array
2022-08-04 14:37:00 【OceanStar's study notes】
题目来源
题目描述

题目解析
快排
The original array is requiredkBig problems can be converted into firstsize - k + 1小的问题
思路
- Randomly select a number from an unordered arrayV,然后对VDo the Dutch flag problem for the entire array.此后,<V的在左边,=V的在中间,>V的在右边
- stop if hit,如果没有命中,Then go back to the first step according to the actual choice of left or right
- 时间复杂度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;
}
};
优先级队列
思路
- The only one has a length of k的小根堆,Keep the element clock in the queue at k个
- 遍历数组nums,After enqueuing an element,Pops the smallest element at the top of the heap immediately
- 遍历完成之后,堆顶的元素就是第k大的数字
ps:求第kLarge numbers are piled with small roots,求第kSmall numbers are piled up with large roots
时间复杂度O(N*logK)
什么时候用?
在数据量很大的时候,可以实现「在线算法」,No need to read all the data into memory at once.
实现
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();
}
};
边栏推荐
猜你喜欢

ASA归因:如何评估关键词的投放价值
![[Beiya data recovery] IBM System Storage storage lvm information lost data recovery solution](/img/1c/3c8c323e6ee3406d202e07f85bab21.jpg)
[Beiya data recovery] IBM System Storage storage lvm information lost data recovery solution

leetcode:251. 展开二维向量

Basic Introduction for PLSQL

Redis 复习计划 - Redis主从数据一致性和哨兵机制

理论篇1:深度学习之----LetNet模型详解

1403. 非递增顺序的最小子序列

【北亚数据恢复】IBM System Storage存储lvm信息丢失数据恢复方案

九州云出席领航者线上论坛,共话5G MEC边缘计算现状、挑战和未来

快解析结合千方百剂
随机推荐
AlphaFold 如何实现 AI 在结构生物学中的全部潜力
js深拷贝和浅拷贝具体使用区别_es6深拷贝和浅拷贝
leetcode:253. 至少需要多少间会议室
Database recovery
leetcode:241. 为运算表达式设计优先级
CF1527D MEX Tree (mex & tree & inclusive)
如何和程序员谈恋爱
字符串类的设计与实现_C语言字符串编程题
word2003按空格键为什么会出现小数点
【HMS core】【Media】【视频编辑服务】 在线素材无法展示,一直Loading状态或是网络异常
并发程序的隐藏杀手——假共享(False Sharing)
Oracle database user creation, restart, import and export
JCMsuite应用:倾斜平面波传播透过光阑的传输
G. Mountaineering Squad (violence & dfs)
[Opportunity Enlightenment-60]: "Soldiers, Stupid Ways"-1- Opening: "Death" and "Life" are the way of heaven
量化细胞内的信息流:机器学习时代下的研究进展
B.构造一个简单的数列(贪心)
OAID是什么
Makefile 语法及使用笔记
快解析结合千方百剂