给你一个整数数组 nums
和一个整数 k
,请你返回其中出现频率前 k
高的元素。你可以按 任意顺序 返回答案。
示例 1:
输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2]
示例 2:
输入: nums = [1], k = 1 输出: [1]
提示:
1 <= nums.length <= 105
k
的取值范围是[1, 数组中不相同的元素的个数]
- 题目数据保证答案唯一,换句话说,数组中前
k
个高频元素的集合是唯一的
进阶:你所设计算法的时间复杂度 必须 优于 O(n log n)
,其中 n
是数组大小。
经典 Top K 问题,可以用堆解决
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
counter = Counter(nums)
hp = []
for num, freq in counter.items():
if len(hp) == k:
heappush(hp, (freq, num))
heappop(hp)
else:
heappush(hp, (freq, num))
return [t[1] for t in hp]
class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Long> frequency = Arrays.stream(nums).boxed()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
Queue<Map.Entry<Integer, Long>> queue = new PriorityQueue<>(Map.Entry.comparingByValue());
for (Map.Entry<Integer, Long> entry : frequency.entrySet()) {
long count = entry.getValue();
if (queue.size() == k) {
if (count > queue.peek().getValue()) {
queue.poll();
queue.offer(entry);
}
} else {
queue.offer(entry);
}
}
return queue.stream().mapToInt(Map.Entry::getKey).toArray();
}
}
class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> counter = new HashMap<>();
for (int num : nums) {
counter.put(num, counter.getOrDefault(num, 0) + 1);
}
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
counter.forEach((num, freq) -> {
if (pq.size() == k) {
pq.offer(new int[]{num, freq});
pq.poll();
} else {
pq.offer(new int[]{num, freq});
}
});
return pq.stream().mapToInt(e -> e[0]).toArray();
}
}
function topKFrequent(nums: number[], k: number): number[] {
let hashMap = new Map();
for (let num of nums) {
hashMap.set(num, (hashMap.get(num) || 0) + 1);
}
let list = [...hashMap];
list.sort((a, b) => b[1] - a[1]);
let ans = [];
for (let i = 0; i < k; i++) {
ans.push(list[i][0]);
}
return ans;
}
function topKFrequent(nums: number[], k: number): number[] {
const map = new Map<number, number>();
let maxCount = 0;
for (const num of nums) {
map.set(num, (map.get(num) ?? 0) + 1);
maxCount = Math.max(maxCount, map.get(num));
}
const res = [];
while (k > 0) {
for (const key of map.keys()) {
if (map.get(key) === maxCount) {
res.push(key);
k--;
}
}
maxCount--;
}
return res;
}
class Solution {
public:
static bool cmp(pair<int, int>& m, pair<int, int>& n) {
return m.second > n.second;
}
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> counter;
for (auto& e : nums) ++counter[e];
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(&cmp)> pq(cmp);
for (auto& [num, freq] : counter)
{
if (pq.size() == k)
{
pq.emplace(num, freq);
pq.pop();
}
else pq.emplace(num, freq);
}
vector<int> ans;
while (!pq.empty())
{
ans.push_back(pq.top().first);
pq.pop();
}
return ans;
}
};
use std::collections::HashMap;
impl Solution {
pub fn top_k_frequent(nums: Vec<i32>, k: i32) -> Vec<i32> {
let mut map = HashMap::new();
let mut max_count = 0;
for &num in nums.iter() {
let val = map.get(&num).unwrap_or(&0) + 1;
map.insert(num, val);
max_count = max_count.max(val);
}
let mut k = k as usize;
let mut res = vec![0; k];
while k > 0 {
let mut next = 0;
for key in map.keys() {
let val = map[key];
if val == max_count {
res[k - 1] = *key;
k -= 1;
} else if val < max_count {
next = next.max(val);
}
}
max_count = next;
}
res
}
}