Skip to content

Commit c793e10

Browse files
committed
feat: add solutions to lc problem: No.0692
No.0692.Top K Frequent Words
1 parent 33881af commit c793e10

File tree

6 files changed

+157
-60
lines changed

6 files changed

+157
-60
lines changed

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

+57-20
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
**方法一:哈希表 + 排序**
53+
5254
<!-- tabs:start -->
5355

5456
### **Python3**
@@ -58,9 +60,8 @@
5860
```python
5961
class Solution:
6062
def topKFrequent(self, words: List[str], k: int) -> List[str]:
61-
counter = Counter(words)
62-
res = sorted(counter, key=lambda word: (-counter[word], word))
63-
return res[:k]
63+
cnt = Counter(words)
64+
return sorted(cnt, key=lambda x: (-cnt[x], x))[:k]
6465
```
6566

6667
### **Java**
@@ -70,32 +71,68 @@ class Solution:
7071
```java
7172
class Solution {
7273
public List<String> topKFrequent(String[] words, int k) {
73-
Map<String, Integer> counter = new HashMap<>();
74-
for (String word : words) {
75-
counter.put(word, counter.getOrDefault(word, 0) + 1);
74+
Map<String, Integer> cnt = new HashMap<>();
75+
for (String v : words) {
76+
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
7677
}
77-
PriorityQueue<String> minHeap = new PriorityQueue<>((a, b) -> {
78-
if (counter.get(a).equals(counter.get(b))) {
79-
return b.compareTo(a);
80-
}
81-
return counter.get(a) - counter.get(b);
78+
PriorityQueue<String> q = new PriorityQueue<>((a, b) -> {
79+
int d = cnt.get(a) - cnt.get(b);
80+
return d == 0 ? b.compareTo(a) : d;
8281
});
83-
for (String word : counter.keySet()) {
84-
minHeap.offer(word);
85-
if (minHeap.size() > k) {
86-
minHeap.poll();
82+
for (String v : cnt.keySet()) {
83+
q.offer(v);
84+
if (q.size() > k) {
85+
q.poll();
8786
}
8887
}
89-
List<String> res = new ArrayList<>();
90-
while (!minHeap.isEmpty()) {
91-
res.add(minHeap.poll());
88+
LinkedList<String> ans = new LinkedList<>();
89+
while (!q.isEmpty()) {
90+
ans.addFirst(q.poll());
9291
}
93-
Collections.reverse(res);
94-
return res;
92+
return ans;
9593
}
9694
}
9795
```
9896

97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
vector<string> topKFrequent(vector<string>& words, int k) {
103+
unordered_map<string, int> cnt;
104+
for (auto& v : words) ++cnt[v];
105+
vector<string> ans;
106+
for (auto& [key, _] : cnt) ans.emplace_back(key);
107+
sort(ans.begin(), ans.end(), [&](const string& a, const string& b) -> bool {
108+
return cnt[a] == cnt[b] ? a < b : cnt[a] > cnt[b];
109+
});
110+
ans.erase(ans.begin() + k, ans.end());
111+
return ans;
112+
}
113+
};
114+
```
115+
116+
### **Go**
117+
118+
```go
119+
func topKFrequent(words []string, k int) []string {
120+
cnt := map[string]int{}
121+
for _, v := range words {
122+
cnt[v]++
123+
}
124+
ans := []string{}
125+
for v := range cnt {
126+
ans = append(ans, v)
127+
}
128+
sort.Slice(ans, func(i, j int) bool {
129+
a, b := ans[i], ans[j]
130+
return cnt[a] > cnt[b] || cnt[a] == cnt[b] && a < b
131+
})
132+
return ans[:k]
133+
}
134+
```
135+
99136
### **...**
100137

101138
```

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

+55-20
Original file line numberDiff line numberDiff line change
@@ -48,39 +48,74 @@ Note that &quot;i&quot; comes before &quot;love&quot; due to a lower alphabetica
4848
```python
4949
class Solution:
5050
def topKFrequent(self, words: List[str], k: int) -> List[str]:
51-
counter = Counter(words)
52-
res = sorted(counter, key=lambda word: (-counter[word], word))
53-
return res[:k]
51+
cnt = Counter(words)
52+
return sorted(cnt, key=lambda x: (-cnt[x], x))[:k]
5453
```
5554

5655
### **Java**
5756

5857
```java
5958
class Solution {
6059
public List<String> topKFrequent(String[] words, int k) {
61-
Map<String, Integer> counter = new HashMap<>();
62-
for (String word : words) {
63-
counter.put(word, counter.getOrDefault(word, 0) + 1);
60+
Map<String, Integer> cnt = new HashMap<>();
61+
for (String v : words) {
62+
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
6463
}
65-
PriorityQueue<String> minHeap = new PriorityQueue<>((a, b) -> {
66-
if (counter.get(a).equals(counter.get(b))) {
67-
return b.compareTo(a);
68-
}
69-
return counter.get(a) - counter.get(b);
64+
PriorityQueue<String> q = new PriorityQueue<>((a, b) -> {
65+
int d = cnt.get(a) - cnt.get(b);
66+
return d == 0 ? b.compareTo(a) : d;
7067
});
71-
for (String word : counter.keySet()) {
72-
minHeap.offer(word);
73-
if (minHeap.size() > k) {
74-
minHeap.poll();
68+
for (String v : cnt.keySet()) {
69+
q.offer(v);
70+
if (q.size() > k) {
71+
q.poll();
7572
}
7673
}
77-
List<String> res = new ArrayList<>();
78-
while (!minHeap.isEmpty()) {
79-
res.add(minHeap.poll());
74+
LinkedList<String> ans = new LinkedList<>();
75+
while (!q.isEmpty()) {
76+
ans.addFirst(q.poll());
8077
}
81-
Collections.reverse(res);
82-
return res;
78+
return ans;
79+
}
80+
}
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
vector<string> topKFrequent(vector<string>& words, int k) {
89+
unordered_map<string, int> cnt;
90+
for (auto& v : words) ++cnt[v];
91+
vector<string> ans;
92+
for (auto& [key, _] : cnt) ans.emplace_back(key);
93+
sort(ans.begin(), ans.end(), [&](const string& a, const string& b) -> bool {
94+
return cnt[a] == cnt[b] ? a < b : cnt[a] > cnt[b];
95+
});
96+
ans.erase(ans.begin() + k, ans.end());
97+
return ans;
8398
}
99+
};
100+
```
101+
102+
### **Go**
103+
104+
```go
105+
func topKFrequent(words []string, k int) []string {
106+
cnt := map[string]int{}
107+
for _, v := range words {
108+
cnt[v]++
109+
}
110+
ans := []string{}
111+
for v := range cnt {
112+
ans = append(ans, v)
113+
}
114+
sort.Slice(ans, func(i, j int) bool {
115+
a, b := ans[i], ans[j]
116+
return cnt[a] > cnt[b] || cnt[a] == cnt[b] && a < b
117+
})
118+
return ans[:k]
84119
}
85120
```
86121

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
vector<string> topKFrequent(vector<string>& words, int k) {
4+
unordered_map<string, int> cnt;
5+
for (auto& v : words) ++cnt[v];
6+
vector<string> ans;
7+
for (auto& [key, _] : cnt) ans.emplace_back(key);
8+
sort(ans.begin(), ans.end(), [&](const string& a, const string& b) -> bool {
9+
return cnt[a] == cnt[b] ? a < b : cnt[a] > cnt[b];
10+
});
11+
ans.erase(ans.begin() + k, ans.end());
12+
return ans;
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func topKFrequent(words []string, k int) []string {
2+
cnt := map[string]int{}
3+
for _, v := range words {
4+
cnt[v]++
5+
}
6+
ans := []string{}
7+
for v := range cnt {
8+
ans = append(ans, v)
9+
}
10+
sort.Slice(ans, func(i, j int) bool {
11+
a, b := ans[i], ans[j]
12+
return cnt[a] > cnt[b] || cnt[a] == cnt[b] && a < b
13+
})
14+
return ans[:k]
15+
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
class Solution {
22
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);
3+
Map<String, Integer> cnt = new HashMap<>();
4+
for (String v : words) {
5+
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
66
}
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);
7+
PriorityQueue<String> q = new PriorityQueue<>((a, b) -> {
8+
int d = cnt.get(a) - cnt.get(b);
9+
return d == 0 ? b.compareTo(a) : d;
1210
});
13-
for (String word : counter.keySet()) {
14-
minHeap.offer(word);
15-
if (minHeap.size() > k) {
16-
minHeap.poll();
11+
for (String v : cnt.keySet()) {
12+
q.offer(v);
13+
if (q.size() > k) {
14+
q.poll();
1715
}
1816
}
19-
List<String> res = new ArrayList<>();
20-
while (!minHeap.isEmpty()) {
21-
res.add(minHeap.poll());
17+
LinkedList<String> ans = new LinkedList<>();
18+
while (!q.isEmpty()) {
19+
ans.addFirst(q.poll());
2220
}
23-
Collections.reverse(res);
24-
return res;
21+
return ans;
2522
}
2623
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class Solution:
22
def topKFrequent(self, words: List[str], k: int) -> List[str]:
3-
counter = Counter(words)
4-
res = sorted(counter, key=lambda word: (-counter[word], word))
5-
return res[:k]
3+
cnt = Counter(words)
4+
return sorted(cnt, key=lambda x: (-cnt[x], x))[:k]

0 commit comments

Comments
 (0)