当前位置:网站首页>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 .
边栏推荐
- 对一个元素使用多种变形的方法
- [kvm] create virtual machine from kickstart file
- How to solve the problem of store ranking?
- 全屋WiFi方案:Mesh路由器组网和AC+AP
- MySQL gets the maximum value record by field grouping
- 小程序:区域滚动、下拉刷新、上拉加载更多
- Fu Yingna: Yuan universe is the new generation of Internet!
- How to query the submission number of a version
- 安装postgis时报找不到“POSTGIS_VERSION”这个函数
- 有没有大佬帮我看下flink sql连接kafka认证kerberos的参数配置是否有误
猜你喜欢
随机推荐
LCA 板子
一个公司的面试笔记
不会就坚持66天吧 权重生成随机数
Semantic segmentation correlation
9.延迟队列
有一种密码学专用语言叫做ASN.1
A little understanding of pointer, secondary pointer, wild pointer, pointer as function return value
The structure pointer must be initialized, and the pointer must also be initialized
请问,在sql client中,执行insert into select from job时,如何单
The principle of inverse Fourier transform (IFFT) in signal processing
不会就坚持58天吧 实现前缀树
kotlin的List,Map,Set等集合类不指定类型
不会就坚持59天吧 替换单词
RMAN do not mark expired backups
不会就坚持61天吧 最短的单词编码
Machine vision Series 2: vs DLL debugging
伏英娜:元宇宙就是新一代互联网!
openFeign异步调用问题
SVG--loading动画
Don't the JDBC SQL connector of the big guys Flink now support all databases, such as vertica?