当前位置:网站首页>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();
}
};
边栏推荐
- centos7安装mysql急速版
- 【硬件架构的艺术】学习笔记(1)亚稳态的世界
- 基于 Next.js实现在线Excel
- 【北亚数据恢复】IBM System Storage存储lvm信息丢失数据恢复方案
- Technology sharing | Description of the electronic fence function in the integrated dispatching system
- 用了TCP协议,就一定不会丢包吗?
- 企业级优化
- How to fall in love with a programmer
- 【Web技术】1401- 图解 Canvas 入门
- Almost all known protein structures in the world are open sourced by DeepMind
猜你喜欢

杭电校赛(逆袭指数)

Find My技术|防止你的宠物跑丢,苹果Find My技术可以帮到你

【Today in History】August 4: First female Turing Award winner; NVIDIA acquires MediaQ; first Cybersecurity Challenge completed

一看就会的Chromedriver(谷歌浏览器驱动)安装教程

解题-->在线OJ(十八)

Cisco - Small Network Topology (DNS, DHCP, Web Server, Wireless Router)

Cisco-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)

FRED应用:毛细管电泳系统

leetcode:254. 因子的组合

Find My Technology | Prevent your pet from getting lost, Apple Find My technology can help you
随机推荐
How to write SQL statements: the usage of Update, Case, and Select together
数据库恢复
异步编程概览
浙江大学团队使用基于知识图谱的新方法,从空间分辨转录组数据中推断细胞间通信状况
砺夏行动|九州云章津楠:开源不是少数人的运动,大众化才是源泉
How to automatically renew the token after it expires?
基于 Next.js实现在线Excel
C# winforms 输入颜色转换颜色名
Google plug-in. Download contents file is automatically deleted after solution
This week to discuss the user experience: Daedalus Nemo to join Ambire, explore the encryption of the ocean
Lixia Action | Kyushu Yunzhang Jinnan: Open source is not a movement for a few people, popularization is the source
How to fall in love with a programmer
Keycloak 6.0.0 正式发布,身份和访问管理系统
CCF GLCC officially opened | Kyushu Cloud open source experts bring generous bonuses to help universities promote open source
AOSP内置APP特许权限白名单
【历史上的今天】8 月 4 日:第一位图灵奖女性得主;NVIDIA 收购 MediaQ;首届网络安全挑战大赛完成
The Internet of things application development trend
License server system does not support this version of this feature
特殊品种的二次开户验资金额
ICML 2022 | 图神经网络的局部增强