当前位置:网站首页>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 .
边栏推荐
- 14.haproxy+keepalived负载均衡和高可用
- UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0x90 in position 614: ordinal not in range(128)
- opengauss预检查安装
- A little understanding of pointer, secondary pointer, wild pointer, pointer as function return value
- flink-sql 如何设置 sql执行超时时间
- Install the laser of ROS_ scan_ Problems encountered in match library (I)
- Cad2020 introductory learning (2021.4.13)
- C语言力扣第61题之旋转链表。双端队列与构造循环链表
- Fu Yingna: Yuan universe is the new generation of Internet!
- “蔚来杯“2022牛客暑期多校训练营2 H
猜你喜欢

Machine vision Series 2: vs DLL debugging

MPU6050

9.延迟队列

Whole house WiFi solution: mesh router networking and ac+ap

Implementation of jump connection of RESNET (pytorch)
![[hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)](/img/fe/8c707c30c734de7bb76ea68134842c.png)
[hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)

伏英娜:元宇宙就是新一代互联网!

15.federation

Deep learning training strategy -- warming up the learning rate

Can you really write restful API?
随机推荐
Pointer constant and constant pointer
不会就坚持63天吧 最大的异或
数据源是SQL server ,我要配置日期字段 updateDate 最后两天日期的增量数据,做增
索引的最左前缀原理
[kvm] install KVM
RMAN do not mark expired backups
[paper translation] vectornet: encoding HD maps and agent dynamics from vectorized representation
Problems encountered in vscode connection SSH
“蔚来杯“2022牛客暑期多校训练营1 J Serval and Essay(启发式合并)
数据库SQL语句实现数据分解的函数查询
12.优先级队列和惰性队列
Leftmost prefix principle of index
kotlin的List,Map,Set等集合类不指定类型
不会就坚持62天吧 单词之和
10. Fallback message
The function "postgis_version" cannot be found when installing PostGIS
Blood cases caused by < meta charset=UTF-8> -- Analysis of common character codes
编译与链接
Semantic segmentation correlation
opengauss预检查安装