与 703 数据流中第k个大元素思路类似
https://blog.csdn.net/include_not_found_/article/details/104858263
class Solution {
// 堆排序,维护一个大顶堆
public:
vector<int> getLeastNumbers(vector<int>& arr, int k) {
priority_queue<int, vector<int>> pq;
for(int i : arr)
{
pq.push(i);
if(pq.size() > k) pq.pop(); // 保持大小在k
}
vector<int> ans;
while(k--)
{
ans.push_back(pq.top());
pq.pop();
}
return ans;
}
};