当前位置:网站首页>Likou 215 questions, the Kth largest element in an array
Likou 215 questions, the Kth largest element in an array
2022-08-10 21:27:00 【Yingtai Night Snow】
力扣215题,数组中的第K个最大元素
题目描述
给定整数数组 nums 和整数 k,请返回数组中第 k 个最大的元素.
请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素.
你必须设计并实现时间复杂度为 O(n) 的算法解决此问题.
输入输出样例
输入: [3,2,1,5,6,4], k = 2
输出: 5
输入: [3,2,3,1,2,4,5,5,6], k = 4
输出: 4
解法一,暴力解法
//Solution 1 does not consider the time complexity
//时间复杂度为O(nlogn)
int findKthLargest(vector<int>&nums,int k)
{
if(nums.empty())
{
return NULL;
}
sort(nums.begin(),nums.end());
int length=nums.size();
return nums[length-k];
}
时复 O(NlogN) 空复 O(logN)
解法二,使用快速排序,先排序再取值(三路排序)
//Done using the idea of quicksort
int findKthLargest2(vector<int>&nums,int k)
{
quickSorts2(nums,0,nums.size()-1);
return nums[nums.size()-k];
}
// void quickSort(vector<int>&nums,int left,int right)
// {
// if(left>=right)
// {
// return;
// }
// int pivotIndex=partition(nums,left,right);
// quickSort(nums,left,pivotIndex-1);
// quickSort(nums,pivotIndex+1,right);
// }
//划分在顺序数组和逆序数组上表现很差的
//解决方法随机选择切分元素
void randomPartiton(vector<int>&nums,int left,int right)
{
int i=left+rand()%(right-left+1);
swap(nums,i,left);
}
void quickSorts2(vector<int>&nums,int left,int right)
{
//当左边大于右边,那么就可以跳出循环
if(left>=right)
{
return;
}
randomPartiton(nums,left,right);
int pivot=nums[left];
// 将pivot后面的元素划分成三个区间
//all in nums[left+1,lt)<pivot;
//all in nums[lt,i)=pivot;
//all in nums(gt,right]>pivot;
//设定边界的初始条件
int lt=left+1;//lt=less than
int gt=right; //gt= greater than
int i=left+1;
while(i<=gt)
{
//当元素小于当前划分的值时
if(nums[i]<pivot)
{
//交换到等于pivot区间的第一个位置
swap(nums,i,lt);
lt++;
i++;
}
else if(nums[i]==pivot)
{
//划分在第二个区间里
i++;
}
else{
//划分到数组的末尾
swap(nums,i,gt);
gt--;
}
}
//交换 pivot
swap(nums,left,lt-1);
//继续递归进行排序
quickSorts2(nums,left,lt-2);
quickSorts2(nums,gt+1,right);
}
void swap(vector<int>&nums,int index1,int index2)
{
int temp=nums[index1];
nums[index1]=nums[index2];
nums[index2]=temp;
}
解法三,Algorithm for subtraction using quicksort(双端)
void swap(vector<int>&nums,int index1,int index2)
{
int temp=nums[index1];
nums[index1]=nums[index2];
nums[index2]=temp;
}
//借用快速排序的思想,Use mitigation methods
//According to the titlek个元素,That means to findpivot恰好等于length-k;
int findKthLargest3(vector<int>&nums,int k)
{
//初始化参数
int length=nums.size();
int left=0;
int right=nums.size()-1;
int target=length-k;
//Use the idea of two-way quick row to find the center
while(left<=right)
{
int pivotIndex=partitionDouble(nums,left,right);
if(pivotIndex==target)
{
return nums[pivotIndex];
}
else if(pivotIndex<target)
{
left=pivotIndex+1;
}
else{
right=pivotIndex-1;
}
}
}
int partitionDouble(vector<int>&nums,int left,int right)
{
//随机选取
randomPartiton(nums,left,right);
int pivot=nums[left];
//Set two intervals
//[left+1,le)<=pivot;
//(ge,right]>=pivot;
//Initialize both ranges so that the original is empty
int le=left+1;
int ge=right;
while(true)
{
//le遇到比pivot大的停下来
while(le<=ge&&nums[le]<pivot)
{
le++;
}
//ge遇到比pivotSmall break to come
while(le<=ge&&nums[ge]>pivot)
{
ge--;
}
if(le>=ge)
{
break;
}
//交换二者的值
swap(nums,le,ge);
le++;
ge--;
}
//交换pivot
swap(nums,left,ge);
return ge;
}
时复:O(n)
空复:O(logn)
解法四,Borrowed from heap sort(最大堆)
//Use heap sort
int findKthLargest4(vector<int>&nums,int k)
{
int heapSize=nums.size();
//建立堆
buildMaxHeap(nums,heapSize);
//Cull from the max heap,and gradually rebuild
for(int i=nums.size()-1;i>=nums.size()-k+1;i--)
{
swap(nums,0,i);
heapSize--;
maxHeapiy(nums,0,heapSize);
}
return nums[0];
}
void buildMaxHeap(vector<int>&nums,int heapSize)
{
for(int i=heapSize/2;i>=0;i--)
{
maxHeapiy(nums,i,heapSize);
}
}
void maxHeapiy(vector<int>&nums,int i,int heapSize)
{
int left=i*2+1;
int right=i*2+2;
int largest=i;
if(left<heapSize&&nums[left]>nums[largest])
{
largest=left;
}
if(right<heapSize&&nums[right]>nums[largest])
{
largest=right;
}
if(largest!=i)
{
swap(nums,i,largest);
maxHeapiy(nums,largest,heapSize);
}
}
时复O(nlogn)
空复O(logn)
边栏推荐
- 社区分享|货拉拉通过JumpServer纳管大规模云上资产
- Live Classroom System 09--Tencent Cloud VOD Management Module (1)
- 使用SylixOS虚拟串口,实现系统串口自由
- C. Rotation Matching
- RADIUS Authentication Server Deployment Costs That Administrators Must Know
- 查询:复杂查询的语法和使用例——《mysql 从入门到内卷再到入土》
- Application of Spatial 3D Model Reconstruction Based on Pix4Dmapper - Spatial Analysis and Site Selection
- 管理员必须知道的RADIUS认证服务器的部署成本
- 内置模板市场,DataEase开源数据可视化分析平台v1.13.0发布
- 2022.8.8好题选讲(数论场)
猜你喜欢
Future与CompletableFuture
【实用软件】【VSCode】使用技巧大全(持续更新)
直播课堂系统08-腾讯云对象存储和课程分类管理
石油化工行业商业供应链管理系统:标准化供应商管理,优化企业供应链采购流程
美创科技勒索病毒“零信任”防护和数据安全治理体系的探索实践
Huawei router clock near the drainage experiment (using stream strategy)
Live Classroom System 08 Supplement - Tencent Cloud Object Storage and Course Classification Management
管理员必须知道的RADIUS认证服务器的部署成本
Date picker component (restrict year to set only displayed months)
RADIUS Authentication Server Deployment Costs That Administrators Must Know
随机推荐
C. Social Distance
【PCBA方案】电子握力测试仪方案she‘ji
D. Game With Array
INSERT:插入操作语法&使用例——《mysql 从入门到内卷再到入土》
将视图模型转换为使用 Hilt 依赖注入
2021年中国工业互联网安全大赛(福建省选拔赛) 暨首届福建省工业互联网创新大赛
UPDATE:修改数据语法使用例——《mysql 从入门到内卷再到入土》
【PCBA solution】Electronic grip strength tester solution she'ji
[Golang]如何优雅管理系统中的几十个UDF(API)
Future-oriented IT infrastructure management architecture - Unified IaaS
如何提高代码的可读性 学习笔记
The use of TortoiseSVN little turtle
石油化工行业商业供应链管理系统:标准化供应商管理,优化企业供应链采购流程
B. Codeforces Subsequences
第14届全国大学生信息安全竞赛-创新实践能力赛
F. Binary String Reconstruction
Auto.js中的悬浮窗
华为路由器旁挂引流实验(使用流策略)
【PCBA方案设计】蓝牙跳绳方案
JS中的filter、map、reduce