2023-06-12 23:04:01 +08:00
|
|
|
/**
|
|
|
|
* File: top_k.java
|
|
|
|
* Created Time: 2023-06-12
|
2024-02-07 22:21:18 +08:00
|
|
|
* Author: krahets (krahets@163.com)
|
2023-06-12 23:04:01 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
package chapter_heap;
|
|
|
|
|
|
|
|
import utils.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public class top_k {
|
|
|
|
/* 基于堆查找数组中最大的 k 个元素 */
|
|
|
|
static Queue<Integer> topKHeap(int[] nums, int k) {
|
2023-10-26 02:54:19 +08:00
|
|
|
// 初始化小顶堆
|
2023-06-12 23:04:01 +08:00
|
|
|
Queue<Integer> heap = new PriorityQueue<Integer>();
|
|
|
|
// 将数组的前 k 个元素入堆
|
|
|
|
for (int i = 0; i < k; i++) {
|
2023-08-10 11:35:16 +08:00
|
|
|
heap.offer(nums[i]);
|
2023-06-12 23:04:01 +08:00
|
|
|
}
|
|
|
|
// 从第 k+1 个元素开始,保持堆的长度为 k
|
|
|
|
for (int i = k; i < nums.length; i++) {
|
|
|
|
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
|
|
|
if (nums[i] > heap.peek()) {
|
|
|
|
heap.poll();
|
2023-08-10 11:35:16 +08:00
|
|
|
heap.offer(nums[i]);
|
2023-06-12 23:04:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return heap;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
int[] nums = { 1, 7, 6, 3, 2 };
|
|
|
|
int k = 3;
|
|
|
|
|
|
|
|
Queue<Integer> res = topKHeap(nums, k);
|
|
|
|
System.out.println("最大的 " + k + " 个元素为");
|
|
|
|
PrintUtil.printHeap(res);
|
|
|
|
}
|
|
|
|
}
|