Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8df828c

Browse files
committedMar 25, 2025·
二刷347
1 parent d2c08f6 commit 8df828c

File tree

4 files changed

+88
-19
lines changed

4 files changed

+88
-19
lines changed
 
Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,61 @@
11
[#0347-top-k-frequent-elements]
2-
= 347. Top K Frequent Elements
2+
= 347. K 个高频元素
33

4-
{leetcode}/problems/top-k-frequent-elements/[LeetCode - Top K Frequent Elements^]
4+
https://leetcode.cn/problems/top-k-frequent-elements/[LeetCode - 347. 前 K 个高频元素 ^]
55

6-
利用桶排序实在是妙妙妙啊!
6+
给你一个整数数组 `nums` 和一个整数 `k` ,请你返回其中出现频率前 `k` 高的元素。你可以按 *任意顺序* 返回答案。
77

8-
Given a non-empty array of integers, return the *_k_* most frequent elements.
8+
*示例 1:*
99

10-
*Example 1:*
10+
....
11+
输入: nums = [1,1,1,2,2,3], k = 2
12+
输出: [1,2]
13+
....
1114

12-
[subs="verbatim,quotes,macros"]
13-
----
14-
*Input:* nums = [1,1,1,2,2,3], k = 2
15-
*Output:* [1,2]
16-
----
15+
*示例 2:*
1716

17+
....
18+
输入: nums = [1], k = 1
19+
输出: [1]
20+
....
1821

19-
*Example 2:*
2022

21-
[subs="verbatim,quotes,macros"]
22-
----
23-
*Input:* nums = [1], k = 1
24-
*Output:* [1]
25-
----
23+
*提示:*
2624

25+
* `1 \<= nums.length \<= 10^5^`
26+
* `k` 的取值范围是 `[1, 数组中不相同的元素的个数]`
27+
* 题目数据保证答案唯一,换句话说,数组中前 `k` 个高频元素的集合是唯一的
2728
28-
*Note: *
29+
**进阶:**你所设计算法的时间复杂度 *必须* 优于 stem:[O(nlogn)] ,其中 `n` 是数组大小。
2930

3031

31-
* You may assume _k_ is always valid, 1 ≤ _k_ ≤ number of unique elements.
32-
* Your algorithm's time complexity *must be* better than O(_n_ log _n_), where _n_ is the array's size.
32+
== 思路分析
3333

34+
利用桶排序实在是妙妙妙啊:使用哈希表统计频率,统计完成后,创建一个数组,将频率作为数组下标,对于出现频率不同的数字集合,存入对应的数组下标即可。
3435

36+
Top K 问题!
3537

3638

3739
[[src-0347]]
40+
[tabs]
41+
====
42+
一刷::
43+
+
44+
--
3845
[{java_src_attr}]
3946
----
4047
include::{sourcedir}/_0347_TopKFrequentElements.java[tag=answer]
4148
----
49+
--
50+
51+
二刷::
52+
+
53+
--
54+
[{java_src_attr}]
55+
----
56+
include::{sourcedir}/_0347_TopKFrequentElements_2.java[tag=answer]
57+
----
58+
--
59+
====
60+
4261

‎logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ endif::[]
6565
|{doc_base_url}/0316-remove-duplicate-letters.adoc[题解]
6666
|❌ 完全想不到单调栈!
6767

68+
|{counter:codes2503}
69+
|{leetcode_base_url}/top-k-frequent-elements/[347. Top K Frequent Elements^]
70+
|{doc_base_url}/0347-top-k-frequent-elements.adoc[题解]
71+
|✅ Top K 问题,优先队列
72+
6873
|===
6974

7075
截止目前,本轮练习一共完成 {codes2503} 道题。

‎src/main/java/com/diguage/algo/leetcode/_0347_TopKFrequentElements.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public class _0347_TopKFrequentElements {
4141
* Memory Usage: 41.2 MB, less than 31.89% of Java online submissions for Top K Frequent Elements.
4242
*
4343
* Copy from: https://leetcode.com/problems/top-k-frequent-elements/discuss/81602/Java-O(n)-Solution-Bucket-Sort[Java O(n) Solution - Bucket Sort - LeetCode Discuss]
44+
*
45+
* @author D瓜哥 · https://www.diguage.com
46+
* @since 2020-01-11 00:04
4447
*/
4548
public List<Integer> topKFrequent(int[] nums, int k) {
4649
Map<Integer, Integer> numToCountMap = new HashMap<>();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.*;
4+
5+
public class _0347_TopKFrequentElements_2 {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2025-03-25 21:20:04
10+
*/
11+
public int[] topKFrequent(int[] nums, int k) {
12+
Map<Integer, Integer> numToCountMap = new HashMap<>();
13+
for (int num : nums) {
14+
Integer count = numToCountMap.getOrDefault(num, 0);
15+
numToCountMap.put(num, ++count);
16+
}
17+
// 寻找最频繁的 K 个元素,这里就要用最小堆。
18+
// 注意:堆里比较的是元素出现的次数,不是元素本身,所以自定义比较器
19+
PriorityQueue<Integer> minHeap = new PriorityQueue<>(Comparator.comparingInt(numToCountMap::get));
20+
for (Map.Entry<Integer, Integer> entry : numToCountMap.entrySet()) {
21+
Integer num = entry.getKey();
22+
Integer count = entry.getValue();
23+
if (minHeap.size() < k) {
24+
minHeap.offer(num);
25+
} else {
26+
if (numToCountMap.get(minHeap.peek()) < count) {
27+
minHeap.poll();
28+
minHeap.offer(num);
29+
}
30+
}
31+
}
32+
int[] result = new int[k];
33+
for (int num : minHeap) {
34+
result[--k] = num;
35+
}
36+
return result;
37+
}
38+
// end::answer[]
39+
public static void main(String[] args) {
40+
new _0347_TopKFrequentElements_2().topKFrequent(new int[]{4, 1, -1, 2, -1, 2, 3}, 2);
41+
}
42+
}

0 commit comments

Comments
 (0)
Please sign in to comment.