Skip to content

Commit 54870da

Browse files
committed
feat: add solutions to lc problem: No.0692. Top K Frequent Words
1 parent 1ab3af8 commit 54870da

File tree

4 files changed

+94
-14
lines changed

4 files changed

+94
-14
lines changed

solution/0600-0699/0692.Top K Frequent Words/README.md

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [692. 前K个高频单词](https://leetcode-cn.com/problems/top-k-frequent-words)
1+
# [692. 前 K 个高频单词](https://leetcode-cn.com/problems/top-k-frequent-words)
22

33
[English Version](/solution/0600-0699/0692.Top%20K%20Frequent%20Words/README_EN.md)
44

@@ -47,7 +47,6 @@
4747
<li>尝试以&nbsp;<em>O</em>(<em>n</em> log <em>k</em>) 时间复杂度和&nbsp;<em>O</em>(<em>n</em>) 空间复杂度解决。</li>
4848
</ol>
4949

50-
5150
## 解法
5251

5352
<!-- 这里可写通用的实现逻辑 -->
@@ -59,15 +58,44 @@
5958
<!-- 这里可写当前语言的特殊实现逻辑 -->
6059

6160
```python
62-
61+
class Solution:
62+
def topKFrequent(self, words: List[str], k: int) -> List[str]:
63+
counter = collections.Counter(words)
64+
res = sorted(counter, key=lambda word: (-counter[word], word))
65+
return res[:k]
6366
```
6467

6568
### **Java**
6669

6770
<!-- 这里可写当前语言的特殊实现逻辑 -->
6871

6972
```java
70-
73+
class Solution {
74+
public List<String> topKFrequent(String[] words, int k) {
75+
Map<String, Integer> counter = new HashMap<>();
76+
for (String word : words) {
77+
counter.put(word, counter.getOrDefault(word, 0) + 1);
78+
}
79+
PriorityQueue<String> minHeap = new PriorityQueue<>((a, b) -> {
80+
if (counter.get(a).equals(counter.get(b))) {
81+
return b.compareTo(a);
82+
}
83+
return counter.get(a) - counter.get(b);
84+
});
85+
for (String word : counter.keySet()) {
86+
minHeap.offer(word);
87+
if (minHeap.size() > k) {
88+
minHeap.poll();
89+
}
90+
}
91+
List<String> res = new ArrayList<>();
92+
while (!minHeap.isEmpty()) {
93+
res.add(minHeap.poll());
94+
}
95+
Collections.reverse(res);
96+
return res;
97+
}
98+
}
7199
```
72100

73101
### **...**

solution/0600-0699/0692.Top K Frequent Words/README_EN.md

+31-10
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
<p>Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first.</p>
1010

11-
12-
1311
<p><b>Example 1:</b><br />
1412

1513
<pre>
@@ -26,8 +24,6 @@
2624

2725
</p>
2826

29-
30-
3127
<p><b>Example 2:</b><br />
3228

3329
<pre>
@@ -44,8 +40,6 @@
4440

4541
</p>
4642

47-
48-
4943
<p><b>Note:</b><br>
5044

5145
<ol>
@@ -58,8 +52,6 @@
5852

5953
</p>
6054

61-
62-
6355
<p><b>Follow up:</b><br />
6456

6557
<ol>
@@ -77,13 +69,42 @@
7769
### **Python3**
7870

7971
```python
80-
72+
class Solution:
73+
def topKFrequent(self, words: List[str], k: int) -> List[str]:
74+
counter = collections.Counter(words)
75+
res = sorted(counter, key=lambda word: (-counter[word], word))
76+
return res[:k]
8177
```
8278

8379
### **Java**
8480

8581
```java
86-
82+
class Solution {
83+
public List<String> topKFrequent(String[] words, int k) {
84+
Map<String, Integer> counter = new HashMap<>();
85+
for (String word : words) {
86+
counter.put(word, counter.getOrDefault(word, 0) + 1);
87+
}
88+
PriorityQueue<String> minHeap = new PriorityQueue<>((a, b) -> {
89+
if (counter.get(a).equals(counter.get(b))) {
90+
return b.compareTo(a);
91+
}
92+
return counter.get(a) - counter.get(b);
93+
});
94+
for (String word : counter.keySet()) {
95+
minHeap.offer(word);
96+
if (minHeap.size() > k) {
97+
minHeap.poll();
98+
}
99+
}
100+
List<String> res = new ArrayList<>();
101+
while (!minHeap.isEmpty()) {
102+
res.add(minHeap.poll());
103+
}
104+
Collections.reverse(res);
105+
return res;
106+
}
107+
}
87108
```
88109

89110
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public List<String> topKFrequent(String[] words, int k) {
3+
Map<String, Integer> counter = new HashMap<>();
4+
for (String word : words) {
5+
counter.put(word, counter.getOrDefault(word, 0) + 1);
6+
}
7+
PriorityQueue<String> minHeap = new PriorityQueue<>((a, b) -> {
8+
if (counter.get(a).equals(counter.get(b))) {
9+
return b.compareTo(a);
10+
}
11+
return counter.get(a) - counter.get(b);
12+
});
13+
for (String word : counter.keySet()) {
14+
minHeap.offer(word);
15+
if (minHeap.size() > k) {
16+
minHeap.poll();
17+
}
18+
}
19+
List<String> res = new ArrayList<>();
20+
while (!minHeap.isEmpty()) {
21+
res.add(minHeap.poll());
22+
}
23+
Collections.reverse(res);
24+
return res;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def topKFrequent(self, words: List[str], k: int) -> List[str]:
3+
counter = collections.Counter(words)
4+
res = sorted(counter, key=lambda word: (-counter[word], word))
5+
return res[:k]

0 commit comments

Comments
 (0)