当前位置:网站首页>Leetcode076 -- the kth largest number in the array
Leetcode076 -- the kth largest number in the array
2022-07-28 10:12:00 【Schuyler_ yuan】
subject :
Given an array of integers
numsAnd integerk, Please return the... In the arraykThe biggest element .Please note that , What you need to look for is the number after array sorting
kThe biggest element , Not the first.kA different element .Example 1:
Input : [3,2,1,5,6,4] and k = 2
Output : 5
Example 2:Input : [3,2,3,1,2,4,5,5,6] and k = 4
Output : 4
Tips :
1 <= k <= nums.length <= 104
-104 <= nums[i] <= 104
Ideas :
The most intuitive solution is to sort the array , Output length-k The number of positions is enough . But there must be unnecessary operations in this way .
Fast scheduling algorithm can refer to : Fast and slow pointer to achieve fast row
How to optimize ?
int findKthLargest(vector<int>& nums, int k) {
quicksort(nums, 0, nums.size() - 1);
return nums[nums.size() - k];
}Optimization idea :
Using the idea of fast platoon , Every round partition Will find an axis value corresponding to the sorted position , When the last position of this axis value is k When , Just output it directly , You don't have to do all the comparisons , The best case is that each axis value is in the median , Two points quick row .
The code is as follows :
int findKthLargest(vector<int>& nums, int k) {
int start = 0, end = nums.size() - 1;
int index = partition(nums, start, end);
while(index != nums.size() - k) {
if (index > nums.size() - k) {
end = index - 1;
index = partition(nums, start, end);
} else if (index < nums.size() - k) {
start = index + 1;
index = partition(nums, start, end);
}
}
return nums[index];
}
int swap(vector<int>& nums, int left, int right) {
int tmp = nums[left];
nums[left] = nums[right];
nums[right] = tmp;
return 0;
}
int partition(vector<int>& nums, int start, int end) {
int index = (start + end) / 2;
swap(nums, index, end);
int small = start - 1;
for (index = start; index < end; index++) {
if (nums[index] < nums[end]) {
++small;
if (small != index) {
swap(nums, small, index);
}
}
}
++small;
swap(nums, small, end);
return small;
}边栏推荐
- 图解 3 种主流企业架构模式(建议收藏!)
- pkg打包node工程
- Mobile number, fixed line regular expression
- Choosing a supplier service system is the first step for large health industry enterprises to move towards digital transformation
- Seektiger eco pass STI new progress, log in to ZB on April 14
- 13 probability distributions that must be understood in deep learning
- 深度学习必懂的 13 种概率分布
- TCP Basics
- uni-app进阶之生命周期
- New features of ES6
猜你喜欢

Performance test of API gateway APIs IX in Google cloud T2a and T2D

ES(8.1)认证题目

为什么要考一级建造师,一建证书含金量有多高?
![[esp32][esp idf] esp32s3 quickly build lvglv7.9](/img/39/8efef047d0a9223b97819a54b5edf8.png)
[esp32][esp idf] esp32s3 quickly build lvglv7.9

Espresso systems, which has just obtained financing, has both intellectual property rights and team ethics in trouble

为报复公司解雇,我更改了项目的所有代码注释!

Guangzhou metro line 14 xinshixu station is under construction, and residents in Baiyun District are about to start a double line transfer mode!
![[OpenHarmony] [RK2206] 构建OpenHarmony编译器 (二)](/img/0c/2e8290403d64ec43d192969f776724.png)
[OpenHarmony] [RK2206] 构建OpenHarmony编译器 (二)
![[learning notes] border and period](/img/a4/5493f7eefc7dd0e38bc9a53a92b87b.png)
[learning notes] border and period

How to get more marks in the game under the new economic model of Plato farm
随机推荐
头文件库文件
LSA and optimization of OSPF
二分、三分、01分数规划 【第I弹】
LinkedList源码按摩,啊舒服
Which strings will be resolved to null by fastjason?
OSPF expansion configuration, routing principles, anti ring and re release
Performance test of API gateway APIs IX in Google cloud T2a and T2D
Mobile number, fixed line regular expression
高温天气筑牢安全生产防线,广州海珠区开展加油站应急演练
Platofarm has made continuous progress, and has launched the official version and super primitive NFT successively
TCP Basics
【云驻共创】企业数字化转型,华为云咨询与你同行
Qt | 信号和槽的一些总结
Seektiger eco pass STI new progress, log in to ZB on April 14
建筑建材行业B2B电子商务网站方案:赋能建材企业转型升级,实现降本提效
【JS高级】js之函数、重载、匿名函数、作用域及作用域链_03
2022-uni-app解析token标准的方式-使用jsrsasign-爬坑过了
19. 删除链表的倒数第 N 个结点
Kubernetes
基于docker安装MySQL