当前位置:网站首页>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;
}边栏推荐
- Platofarm has made continuous progress, and has launched the official version and super primitive NFT successively
- [learning notes] border and period
- 二分、三分、01分数规划【第III弹】
- Introduction to timebasedrollingpolicy
- API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
- 2021.07.13 我们是这样崩的
- OSPF expansion configuration, routing principles, anti ring and re release
- Introduction to consoleappender
- 7.27 minimum spanning tree phased test problem solution
- 深度学习必懂的 13 种概率分布
猜你喜欢

记录一次idea中的父子项目修改project与module名称,亲测!

arthas使用教程

What are the highlights of B2B2C system? How to help jewelry enterprises build an omni channel multi merchant mall management system

API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试

Boss: there are too many systems in the company. Can we realize account interworking?

语音聊天app——如何规范开发流程?

In the era of home health diagnosis, Senzo creates enhanced lateral flow test products

小黑重新站起来看leetcode:653. 两数之和 IV - 输入 BST

软件设计师考前20问,注意啦!!

房地产数字化转型方案:全方位数智化系统运营,助力房企管控实效提升
随机推荐
Read Plato farm's eplato and the reason for its high premium
TCP Basics
02.1.2.逻辑类型 bool
[esp32][esp idf][lvgl7.9] failed to compile with OLED IIC
MySQL 为什么有时候会选错索引?
[openharmony] [rk2206] build openharmony compiler (2)
银行入职考试要点汇总
Espresso systems, which has just obtained financing, has both intellectual property rights and team ethics in trouble
[ESP32][esp-idf] AP+STA实现无线桥接 中转wifi信号
Which strings will be resolved to null by fastjason?
ADVANCE.AI出海指南助力企业出海印尼,掌握东南亚市场半边天
Status Notice ¶
Thinking and summary of technical personnel | R & D Efficiency
【云驻共创】企业数字化转型,华为云咨询与你同行
LinkedList源码按摩,啊舒服
[learning notes] border and period
select如果不加order by子句,返回结果的顺序是不可靠的
Function introduction and description of @jsontype annotation in fastjson
Cloudcompare & PCL matching point sampling consistency suppression
LinkedList source massage, ah comfortable