package 堆相关.q347_前K个高频元素;
import java.util.*;
/**
* 利用大根堆(PriorityQueue)实现 o(n*log(k))
*/
class Solution {
public List<Integer> topKFrequent(int[] nums, int k) {
HashMap<Integer, Integer> count = new HashMap<>();
for (int n : nums) {
count.put(n, count.getOrDefault(n, 0) + 1);
}
PriorityQueue<Integer> heap = new PriorityQueue<>(Comparator.comparingInt(count::get));
for (int n : count.keySet()) {
heap.add(n);
if (heap.size() > k) {
heap.poll();
}
}
List<Integer> topK = new LinkedList<>();
while (!heap.isEmpty()) {
topK.add(heap.poll());
}
Collections.reverse(topK);
return topK;
}
}
q347_前K个高频元素
作品《LeetCode题目分类与面试问题整理 - q347_前K个高频元素》由 不喝星巴克 发布于 匠果,转载请注明出处及链接地址:
http://www.jiangguo.net/c/9r6/457.html