Files
hello-algo/ru/codes/cpp/chapter_heap/top_k.cpp
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

39 lines
1.3 KiB
C++
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.cpp
* Created Time: 2023-06-12
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* Найти k наибольших элементов массива с помощью кучи */
priority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k) {
// Инициализация минимальной кучи
priority_queue<int, vector<int>, greater<int>> heap;
// Поместить первые k элементов массива в кучу
for (int i = 0; i < k; i++) {
heap.push(nums[i]);
}
// Начиная с элемента k+1, поддерживать длину кучи равной k
for (int i = k; i < nums.size(); i++) {
// Если текущий элемент больше элемента на вершине кучи, извлечь вершину кучи и добавить текущий элемент в кучу
if (nums[i] > heap.top()) {
heap.pop();
heap.push(nums[i]);
}
}
return heap;
}
// Driver Code
int main() {
vector<int> nums = {1, 7, 6, 3, 2};
int k = 3;
priority_queue<int, vector<int>, greater<int>> res = topKHeap(nums, k);
cout << "Наибольшие " << k << " элементов: ";
printHeap(res);
return 0;
}