当前位置:网站首页>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();
}
};
边栏推荐
猜你喜欢

The Internet of things application development trend

爬虫——selenium基本使用、无界面浏览器、selenium的其他用法、selenium的cookie、爬虫案例

Google plug-in. Download contents file is automatically deleted after solution

Rust 从入门到精通04-变量

基本介绍PLSQL

Database recovery

This week to discuss the user experience: Daedalus Nemo to join Ambire, explore the encryption of the ocean

CCF GLCC officially opened | Kyushu Cloud open source experts bring generous bonuses to help universities promote open source

ICML 2022 | 图神经网络的局部增强

eNSP-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)
随机推荐
leetcode:259. 较小的三数之和
技术分享| 小程序实现音视频通话
Crawler - basic use of selenium, no interface browser, other uses of selenium, cookies of selenium, crawler cases
CCF GLCC officially opened | Kyushu Cloud open source experts bring generous bonuses to help universities promote open source
[LeetCode] 38. Appearance sequence
X射线掠入射聚焦反射镜
Android Sqlite3基本命令
兆骑科创创新创业大赛活动举办,线上直播路演,投融资对接
C# 复制列表
【Web技术】1401- 图解 Canvas 入门
Oracle database user creation, restart, import and export
ACL 2022 | 社会科学理论驱动的言论建模
【剑指offer33】二叉搜索树的后序遍历序列
杭电校赛(ACM组队安排)
leetcode:253. 至少需要多少间会议室
属于程序猿的浪漫
Sum of four squares, laser bombs
FRED Application: Capillary Electrophoresis System
Why does the decimal point appear when I press the space bar in word 2003?
蓝牙技术|上半年全国新增 130 万台充电桩,蓝牙充电桩将成为市场主流