当前位置:网站首页>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 requiredk
Big 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();
}
};
边栏推荐
猜你喜欢
Hangzhou Electric School Competition (Counter Attack Index)
Lixia Action | Kyushu Yunzhang Jinnan: Open source is not a movement for a few people, popularization is the source
【Today in History】August 4: First female Turing Award winner; NVIDIA acquires MediaQ; first Cybersecurity Challenge completed
ICML 2022 | 图神经网络的局部增强
leetcode:250. 统计同值子树
token 过期后,如何自动续期?
FRED应用:毛细管电泳系统
技术分享| 融合调度系统中的电子围栏功能说明
MySQL【触发器】
【硬件架构的艺术】学习笔记(1)亚稳态的世界
随机推荐
xampp安装包含的组件有(php,perl,apche,mysql)
数据库恢复
Hangzhou Electric School Competition (Counter Attack Index)
Almost all known protein structures in the world are open sourced by DeepMind
SQL语句的写法:Update、Case、 Select 一起的用法
快解析结合友加畅捷U+
ACL 2022 | 社会科学理论驱动的言论建模
Cisco-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)
属于程序猿的浪漫
C# 复制列表
[LeetCode] 38. Appearance sequence
Qt的QItemDelegate使用
B. Construct a simple sequence (greedy)
Problem solving-->Online OJ (18)
杭电校赛(逆袭指数)
leetcode:250. 统计同值子树
本周讨论用户体验:Daedalus 的 Nemo 加入 Ambire,探索加密海洋
一看就会的Chromedriver(谷歌浏览器驱动)安装教程
The Internet of things application development trend
【Today in History】August 4: First female Turing Award winner; NVIDIA acquires MediaQ; first Cybersecurity Challenge completed