当前位置:网站首页>The largest kth element in the array
The largest kth element in the array
2022-06-11 02:14:00 【Lingling Ling】
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/kth-largest-element-in-an-array
Title Description :
Given an array of integers nums And integer k, Please return the... In the array k The biggest element .
Please note that , What you need to look for is the number after array sorting k The biggest element , Not the first. k A different element .
Input :
[3,2,1,5,6,4] and k = 2
Output :
5
Their thinking ( This solution is relatively optimized ):
- utilize priority_queue Build a pile
- priority_queue The default is to create a large number of
- By destacking , You can find the data
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
// Method 1 : Prioritize , Ascending , Look forward
// Time complexity :O(N*logN)
// sort(nums.begin(),nums.end());
// return nums[nums.size()-k];
// Method 2 : Prioritize , Using template parameters , Sort directly in descending order , Look from front to back
// Time complexity :O(N*logN)
//sort(nums.begin(),nums.end(),greater<int>());
//return nums[k-1];
// Method 3 : Put the data in priority_queue in , Before deleting again k-1 Number
// Time complexity :O(N+K*logK)
// if N Far greater than K, This method is more applicable
//priority_queue<int> p(nums.begin(),nums.end())
//for(int i = 0; i < k-1; i++)
//{
// p.pop();
//}
//return p.top();
// Method four :
// structure k A few small piles , optimization
// Time complexity :O(K+(N-K)*logK)
priority_queue<int,vector<int>,greater<int>> KMinHeap(nums.begin(),nums.begin()+k);
for(size_t i = k; i < nums.size(); ++i)
{
if(nums[i] > KMinHeap.top())
{
KMinHeap.pop();
KMinHeap.push(nums[i]);
}
}
return KMinHeap.top();
}
};
边栏推荐
- Task07: double pointer
- 技术分享| 快对讲,全球对讲
- 渗透测试-安全服务体系+OWASP top 10
- The interviewer of Tencent said that who knows the internal module index principle and performance optimization idea of MySQL architecture?
- [penetration test tool bee] how to install and use the XSS penetration test tool bee?
- How to reinstall win11 drawing tool when it is missing
- Wrong question (character array)
- SAP SMARTFORMS文本内容手动换行输出
- Md61 plan independent demand import Bapi [by daily dimension / dynamic template / dynamic field]
- QT widget's simple serial port assistant (qserialport)
猜你喜欢

QT database learning notes (I) basic concepts of database

Task03: building an offline material system

(solved) latex -- cancel the superscript display of references in the text (gbt7714-2015 will lead to the default superscript reference) (tutorial on mixed use of superscript and flush)

Secret

The female programmer gives out a salary slip: the salary is high, but she feels she is 10 years old
![[3.delphi common components] 4 Select class component](/img/36/e78ee0c082bc36be6dbc49d0e12521.jpg)
[3.delphi common components] 4 Select class component

Virtual joystick of QT quick QML instance

Introduction and practice of QT tcp/udp network protocol (I) TCP communication

技术分享| 快对讲,全球对讲

14: 00 interview, came out at 14:08, the question is really too
随机推荐
贵金属白银和现货白银之间是什么关系
ACM tutorial - heap sorting
Task05: tree
NFT Insider #61:Animoca Brands 在 340 项投资中持有 15 亿美元的加密资产
---Arrange numbers---
[penetration test tool bee] how to install and use the XSS penetration test tool bee?
Oracle收集统计信息
How to reinstall win11 drawing tool when it is missing
[leetcode] notes on recursive time value transfer and reference transfer
During SSH configuration key login, you need to pay attention to whether the private key is set with a password
Task02: linked list
Virtual joystick of QT quick QML instance
Sequence table exercises
Return function of different return values
[matlab] image segmentation
Contest2902 - following Tang Kelian's programming: sequence structure question d: area 201502 question f: persistence of supporting college students in Ludian earthquake
Task03: stack
20N10-ASEMI中小功率MOS管20N10
Using an old mobile phone to build a server and achieve intranet penetration does not require root (I have personally tested the simplest one many times)
SAP SMARTFORMS换页打印自动判断