Files
hello-algo/ru/codes/java/chapter_heap/top_k.java
Yudong Jin 772183705e Add ru version (#1865)
* Add Russian docs site baseline

* Add Russian localized codebase

* Polish Russian code wording

* Update ru code translation.

* Update code translation and chapter covers.

* Fix pythontutor extraction.

* Add README and landing page.

* placeholder of profiles

* Use figures of English version

* Remove chapter paperbook
2026-03-28 04:24:07 +08:00

41 lines
1.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* File: top_k.java
* Created Time: 2023-06-12
* Author: krahets (krahets@163.com)
*/
package chapter_heap;
import utils.*;
import java.util.*;
public class top_k {
/* Найти k наибольших элементов массива с помощью кучи */
static Queue<Integer> topKHeap(int[] nums, int k) {
// Инициализация минимальной кучи
Queue<Integer> heap = new PriorityQueue<Integer>();
// Поместить первые k элементов массива в кучу
for (int i = 0; i < k; i++) {
heap.offer(nums[i]);
}
// Начиная с элемента k+1, поддерживать длину кучи равной k
for (int i = k; i < nums.length; i++) {
// Если текущий элемент больше элемента на вершине кучи, извлечь вершину кучи и добавить текущий элемент в кучу
if (nums[i] > heap.peek()) {
heap.poll();
heap.offer(nums[i]);
}
}
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);
}
}