当前位置:网站首页>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();
}
};
边栏推荐
- AOSP内置APP特许权限白名单
- 输入输出流总结
- 四平方和,激光炸弹
- 【历史上的今天】8 月 4 日:第一位图灵奖女性得主;NVIDIA 收购 MediaQ;首届网络安全挑战大赛完成
- 利用决策树找出最优特征组合
- Win10无法访问移动硬盘怎么解决
- JCMsuite应用:倾斜平面波传播透过光阑的传输
- Set partition minimum difference problem (01 knapsack)
- 特殊品种的二次开户验资金额
- [Opportunity Enlightenment-60]: "Soldiers, Stupid Ways"-1- Opening: "Death" and "Life" are the way of heaven
猜你喜欢
随机推荐
word2003按空格键为什么会出现小数点
AOSP built-in APP franchise rights white list
Find My Technology | Prevent your pet from getting lost, Apple Find My technology can help you
CF1527D MEX Tree(mex&树&容斥)
字符串类的设计与实现_C语言字符串编程题
Hangzhou Electric School Competition (Counter Attack Index)
[Opportunity Enlightenment-60]: "Soldiers, Stupid Ways"-1- Opening: "Death" and "Life" are the way of heaven
CF1527D MEX Tree (mex & tree & inclusive)
How to fall in love with a programmer
JCMsuite应用:倾斜平面波传播透过光阑的传输
数据库恢复
Sum of four squares, laser bombs
【Today in History】August 4: First female Turing Award winner; NVIDIA acquires MediaQ; first Cybersecurity Challenge completed
1403. 非递增顺序的最小子序列
G. Mountaineering Squad (violence & dfs)
并发程序的隐藏杀手——假共享(False Sharing)
ASA归因:如何评估关键词的投放价值
centos7安装mysql急速版
如何确定异步 I/O 瓶颈
1375. 二进制字符串前缀一致的次数-前序遍历法









