当前位置:网站首页>Leetcode215 the kth largest element (derivation of quick sort partition function)
Leetcode215 the kth largest element (derivation of quick sort partition function)
2022-07-26 14:05:00 【Lin Shu ໌້ᮨ】
1. problem
Find the second in the array k The biggest element : 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 .

2. Methods to analyze
eg: Example 2 After ordering :122334556,k=4, Here's number k(4) The big element refers to the fourth element from the maximum to the decimal , The output 4【 That is, find the penultimate after sorting k Elements 】, Instead of outputting the fourth different element from big to small 3.
1) Sort (nlogN)
take nums Array sorting , take arr.length-k The element corresponding to this index can .
put questions to : If time complexity is required now <nlogn Well ?
2) Use priority queues (nlogK)
Take the big and use the small , First establish k The smallest heap of elements , Scan the set , Just saved before k The biggest element , At this point, take out the top element .( The top element is the first k Big elements )
3) Use the partition function of fast row (O(n))
Require a set of The first k Big element , It happens to be The... After sorting the set n-k An index The value of the location , The partition function returns the last index after sorting ? Every time an element is delimited between partitions, the position of the element is determined and the corresponding index position of the element is returned , If this index is exactly equal to n-k Location index , Then solve the problem directly .
3. Code implementation
Code implementation based on fast sorting partition function
public class Num251_FindKLargest {
private Random random=new Random();
public int findKthLargest(int[] nums, int k) {
// Use the quick row partition function , When partitioning, find the element index after partitioning and nums.length-k Values with the same index , This value is the value to be found
// Returns the largest element
//nums.length-k: Last but not least k A place ; The first k The largest element is the penultimate element after sorting nums.length-k The elements of
if(k>nums.length){
throw new NoSuchElementException("illegal k,error");
}
return quickSortSelect(nums,0,nums.length-1,nums.length-k);
}
// Choose whether to sort ,== You don't need to sort the situation, just return the value , This question is mainly about evaluation
private int quickSortSelect(int[] nums, int l, int r, int index) {
// If index It is the same as the index of the returned demarcation point , There is no need to continue to line up
int p=partition(nums,l,r);// Return the demarcation point index
if(p==index){
return nums[p];
}
// The sorted partition point index is not equal to the penultimate k An index index
// Partition point subscript <index, Recursive right interval ,>index Recursive left interval
return p<index?quickSortSelect(nums,p+1,r,index):quickSortSelect(nums,l,p-1,index);
}
//nums[l...r] Sort
private int partition(int[] nums, int l, int r) {
int randomIndex=random.nextInt(r-l+1)+l;
swap(nums,l,randomIndex);
int v=nums[l];
//nums[l+1...j]<v
//nums[j+1...i)>=v
int j=l;
//i Is currently traversing the element
for (int i = l+1; i <= r ; i++) {
if(nums[i]<v){
swap(nums,i,j+1);
j++;
}//else It only needs i++, The conditions have been written
}
// After the cycle is completed, the partition is divided , In exchange for l And j Index position element (j Location is <v Last element of )
swap(nums,l,j);
return j;
}
private void swap(int[] nums, int i, int j) {
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
}
边栏推荐
- 数据泄漏、删除事件频发,企业应如何构建安全防线?
- Familiarize you with the "phone book" of cloud network: DNS
- 基于专利多属性融合的技术主题划分方法研究
- Why does WPS refuse advertising?
- Videojs to canvas pause, play, switch video
- 【着色器实现Overlay重新覆盖变装效果_Shader效果第九篇】
- 向路由组件传递参数
- JS timer realizes the countdown and jumps to the login page
- 基址寻址和变址寻址区别
- C语言_结构体和数组的结合
猜你喜欢

android安全基础知识学习

二叉树的层序遍历(C语言实现)

2022年,我们只用一个月就“送走”了这么多互联网产品

Focus on building four "highlands" and join hands with partners to build the national cloud!

Comparison between agile development and Devops

See you tomorrow at the industrial session of cloud intelligence technology forum!

Disease knowledge discovery based on spo semantic triples

ISCC2021 LOCKK题解

敏捷开发与DevOps的对比

Solve the problem that JUnit of idea console cannot be input with scanner
随机推荐
With frequent data leakage and deletion events, how should enterprises build a security defense line?
【数学建模】常用基本模型总结
循环队列(c语言实现)
Integer internal cache
Pytorch学习笔记(一)安装与常用函数的使用
Understand the meaning of length in MySQL data types
ISCC2021 LOCKK题解
【论文阅读】GRAW+:A Two-View Graph Propagation Method With Word Coupling for Readability Assessment
[noip2003 popularity group] stack
Flink SQL(三) 连接到外部系统System和JDBC
C语言_结构体和数组的结合
Frisbee, 2022 "black red" top stream
2022年,我们只用一个月就“送走”了这么多互联网产品
力扣------字符串中的单词数
一文学透MySQL表的创建和约束
My meeting of OA project
Pytoch learning notes (I) installation and use of common functions
「中高级试题」:MVCC实现原理是什么?
Detailed explanation of alter field of MySQL Foundation
Videojs to canvas pause, play, switch video