当前位置:网站首页>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;
}边栏推荐
- Data can't lie. Plato farm is the leader of the meta universe
- arthas使用教程
- ADVANCE.AI出海指南助力企业出海印尼,掌握东南亚市场半边天
- [jzof] 14 cut rope
- 【Gradle】This version of the JMH Gradle plugin requires Gradle 6+, you are using 6.6.
- Redis面试题必知必会
- [learning notes] border and period
- Joint search set
- 二维前缀和
- [esp32][esp idf] esp32s3 quickly build lvglv7.9
猜你喜欢

为什么要考一级建造师,一建证书含金量有多高?
![[esp32][esp idf] ap+sta realizes wireless bridging and transferring WiFi signals](/img/bf/0a968064a8f7c11b86a2a2820208e6.png)
[esp32][esp idf] ap+sta realizes wireless bridging and transferring WiFi signals

LSA and optimization of OSPF

房地产数字化转型方案:全方位数智化系统运营,助力房企管控实效提升

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

In hot weather, the line of defense for safe production was strengthened, and Guangzhou Haizhu District carried out emergency drills for gas stations

【JS高级】js之函数、重载、匿名函数、作用域及作用域链_03
Edge team explains how to improve the comprehensive performance experience through disk cache compression technology

2022-uni-app解析token标准的方式-使用jsrsasign-爬坑过了

Xiao Hei stands up again and looks at leetcode:653. Sum of two IV - enter BST
随机推荐
【MySQL】查询多个ID返回字符串拼接
二分、三分、01分数规划 【第I弹】
【JZOF】15二进制中1的位数
B2B e-commerce website scheme for building materials industry: enable the transformation and upgrading of building materials enterprises to achieve cost reduction and efficiency improvement
Qt | 信号和槽的一些总结
Mobile number, fixed line regular expression
巧用ngx_lua做流量分组
Function introduction and description of @jsontype annotation in fastjson
CGAL编译错误
LinkedList源码按摩,啊舒服
Fixedwindowrollingpolicy introduction
1. 两数之和
ES(8.1)认证题目
Data can't lie. Plato farm is the leader of the meta universe
选择供应商服务系统,是大健康产业企业迈向数字化转型的第一步
OSPF的不规则区域,LSA和序列号
02.1.2.逻辑类型 bool
Several innovative economic models of platofarm have inspired the current metacosmic market
Talk about the problem of preventing others from debugging websites through console based on JS implementation
2022 uni app parsing token standard - use jsrsasign - climb the pit