当前位置:网站首页>It won't last for 70 days. The k-largest number in the array
It won't last for 70 days. The k-largest number in the array
2022-07-29 04:16:00 【A little Ming】
Calling array sort Api
In ascending order , Take the penultimate k Elements .
Time complexity :O(n*logn)
class Solution {
public int findKthLargest(int[] nums, int k) {
Arrays.sort(nums);
return nums[nums.length - k];
}
}
author :fen-zi-yun-he
link :https://leetcode.cn/problems/xx4gT2/solution/-by-fen-zi-yun-he-4qjy/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
Use priority queues PriorityQueue
PriorityQueue The default is ascending , So if PriorityQueue There are more than k individual , Just remove the smallest element in the pile .
The last remaining k The first of the elements , It's what you want .
Time complexity :O(n*logn), Spatial complexity :O(n)
class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
for(int i = 0; i < nums.length; i ++) {
pq.offer(nums[i]);
if(pq.size() > k) {
// If the stack storage quantity exceeds k, Remove the smallest element of the stack
pq.poll();
}
}
return pq.poll();
}
}
author :fen-zi-yun-he
link :https://leetcode.cn/problems/xx4gT2/solution/-by-fen-zi-yun-he-4qjy/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
Sort by hand
Time complexity :O(n*logn), Spatial complexity :O(n)
class Solution {
public int findKthLargest(int[] nums, int k) {
nums = queueSort(nums, 0, nums.length - 1);
return nums[nums.length - k];
}
private int[] queueSort(int[] nums, int left, int right) {
if(left == right) {
// This is not a return nums, Instead, it returns a length of 1 New array
return new int[]{nums[left]};
}
int mid = left + (right - left >> 1);
int[] leftArr = queueSort(nums, left, mid);
int[] rightArr = queueSort(nums, mid + 1, right);
return merge(leftArr, rightArr);
}
private int[] merge(int[] left, int[] right) {
int len1 = left.length, len2 = right.length;
int[] tmp = new int[len1 + len2];
int i = 0, j = 0;
int idx = 0;
while(i < len1 && j < len2) {
if(left[i] < right[j]) {
tmp[idx ++] = left[i ++];
} else {
tmp[idx ++] = right[j ++];
}
}
while(i < len1) {
tmp[idx ++] = left[i ++];
}
while(j < len2) {
tmp[idx ++] = right[j ++];
}
return tmp;
}
}
author :fen-zi-yun-he
link :https://leetcode.cn/problems/xx4gT2/solution/-by-fen-zi-yun-he-4qjy/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
Sort by handwriting pile
Time complexity :O(n*logn), Spatial complexity :O(1)
class Solution {
public int findKthLargest(int[] nums, int k) {
for(int i = 0; i < nums.length; i ++) {
heapInsert(nums, i);
}
int size = nums.length;
if(k == 1) {
return nums[0];
} else {
for(int i = 0; i < k - 1; i ++) {
swap(nums, 0, -- size);
heapify(nums, 0, size);
}
return nums[0];
}
}
private void heapInsert(int[] nums, int index) {
while(nums[index] > nums[(index - 1) / 2]) {
swap(nums, index, (index - 1) / 2);
index = (index - 1) / 2;
}
}
private void heapify(int[] nums, int index, int size) {
int largest = 2 * index + 1;
// It should not be written here index < size
while(largest < size) {
largest = largest + 1 < size && nums[largest + 1] > nums[largest] ? largest + 1 : largest;
largest = nums[index] > nums[largest] ? index : largest;
if(largest == index) {
break;
}
swap(nums, index, largest);
index = largest;
largest = 2 * index + 1;
}
}
private void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
author :fen-zi-yun-he
link :https://leetcode.cn/problems/xx4gT2/solution/-by-fen-zi-yun-he-4qjy/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
边栏推荐
- 数据源是SQL server ,我要配置日期字段 updateDate 最后两天日期的增量数据,做增
- When array is used as a function parameter, it is better to use the array size as a function parameter
- Machine vision series 3:vs2019 opencv environment configuration
- How to write the filter conditions of data integration and what syntax to use? SQL syntax processing bizdate can not be
- 2021 sist summer camp experience + record post of School of information, Shanghai University of science and technology
- How to set the SQL execution timeout for flick SQL
- Opengauss pre check installation
- Object detection: object_ Detection API +ssd target detection model
- Common components of solder pad (2021.4.6)
- “蔚来杯“2022牛客暑期多校训练营2 H
猜你喜欢

Copy products with one click from Taobao, tmall, 1688, wechat, jd.com, Suning, taote and other platforms to pinduoduo platform (batch upload baby details Interface tutorial)

9.延迟队列

Can you really write restful API?

Record of problems encountered in ROS learning

The solution of porting stm32f103zet6 program to c8t6+c8t6 download program flash timeout

不会就坚持64天吧 查找插入位置

不会就坚持66天吧 权重生成随机数

Two forms of softmax cross entropy + numpy implementation

C语言力扣第61题之旋转链表。双端队列与构造循环链表

全屋WiFi方案:Mesh路由器组网和AC+AP
随机推荐
不会就坚持70天吧 数组中第k大的数
店铺排名问题,如何解决?
数据源是SQL server ,我要配置日期字段 updateDate 最后两天日期的增量数据,做增
The principle of inverse Fourier transform (IFFT) in signal processing
Pointer constant and constant pointer
Multi card training in pytorch
The pit I walked through: the first ad Sketchpad
Is the array name a pointer
不会就坚持67天吧 平方根
Kotlin's list, map, set and other collection classes do not specify types
2021 sist summer camp experience + record post of School of information, Shanghai University of science and technology
A little understanding of pointer, secondary pointer, wild pointer, pointer as function return value
不会就坚持59天吧 替换单词
编译与链接
数据集成这个地方的过滤条件该咋写,用的啥语法?sql语法处理bizdate可以不
Differences and principles of bio, NiO and AIO
MPU6050
14.haproxy+keepalived负载均衡和高可用
opengauss预检查安装
What the hell is this error? It doesn't affect the execution result, but it always reports errors when executing SQL... Connecting maxcomputer uses