Skip to content

Commit e78da54

Browse files
authored
feat: add solutions to lc problem: No.0692 (#3760)
No.0692.Top K Frequent Words
1 parent 30206e2 commit e78da54

File tree

6 files changed

+131
-88
lines changed

6 files changed

+131
-88
lines changed

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

+46-28
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ tags:
6969

7070
### 方法一:哈希表 + 排序
7171

72+
我们可以用一个哈希表 $\textit{cnt}$ 记录每一个单词出现的次数,然后对哈希表中的键值对按照值进行排序,如果值相同,按照键进行排序。
73+
74+
最后取出前 $k$ 个键即可。
75+
76+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为单词的个数。
77+
7278
<!-- tabs:start -->
7379

7480
#### Python3
@@ -86,23 +92,19 @@ class Solution:
8692
class Solution {
8793
public List<String> topKFrequent(String[] words, int k) {
8894
Map<String, Integer> cnt = new HashMap<>();
89-
for (String v : words) {
90-
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
95+
for (String w : words) {
96+
cnt.merge(w, 1, Integer::sum);
9197
}
92-
PriorityQueue<String> q = new PriorityQueue<>((a, b) -> {
93-
int d = cnt.get(a) - cnt.get(b);
94-
return d == 0 ? b.compareTo(a) : d;
98+
Arrays.sort(words, (a, b) -> {
99+
int c1 = cnt.get(a), c2 = cnt.get(b);
100+
return c1 == c2 ? a.compareTo(b) : c2 - c1;
95101
});
96-
for (String v : cnt.keySet()) {
97-
q.offer(v);
98-
if (q.size() > k) {
99-
q.poll();
102+
List<String> ans = new ArrayList<>();
103+
for (int i = 0; i < words.length && ans.size() < k; ++i) {
104+
if (i == 0 || !words[i].equals(words[i - 1])) {
105+
ans.add(words[i]);
100106
}
101107
}
102-
LinkedList<String> ans = new LinkedList<>();
103-
while (!q.isEmpty()) {
104-
ans.addFirst(q.poll());
105-
}
106108
return ans;
107109
}
108110
}
@@ -115,13 +117,17 @@ class Solution {
115117
public:
116118
vector<string> topKFrequent(vector<string>& words, int k) {
117119
unordered_map<string, int> cnt;
118-
for (auto& v : words) ++cnt[v];
120+
for (const auto& w : words) {
121+
++cnt[w];
122+
}
119123
vector<string> ans;
120-
for (auto& [key, _] : cnt) ans.emplace_back(key);
121-
sort(ans.begin(), ans.end(), [&](const string& a, const string& b) -> bool {
122-
return cnt[a] == cnt[b] ? a < b : cnt[a] > cnt[b];
124+
for (const auto& [w, _] : cnt) {
125+
ans.push_back(w);
126+
}
127+
ranges::sort(ans, [&](const string& a, const string& b) {
128+
return cnt[a] > cnt[b] || (cnt[a] == cnt[b] && a < b);
123129
});
124-
ans.erase(ans.begin() + k, ans.end());
130+
ans.resize(k);
125131
return ans;
126132
}
127133
};
@@ -130,23 +136,35 @@ public:
130136
#### Go
131137
132138
```go
133-
func topKFrequent(words []string, k int) []string {
139+
func topKFrequent(words []string, k int) (ans []string) {
134140
cnt := map[string]int{}
135-
for _, v := range words {
136-
cnt[v]++
141+
for _, w := range words {
142+
cnt[w]++
137143
}
138-
ans := []string{}
139-
for v := range cnt {
140-
ans = append(ans, v)
144+
for w := range cnt {
145+
ans = append(ans, w)
141146
}
142-
sort.Slice(ans, func(i, j int) bool {
143-
a, b := ans[i], ans[j]
144-
return cnt[a] > cnt[b] || cnt[a] == cnt[b] && a < b
145-
})
147+
sort.Slice(ans, func(i, j int) bool { a, b := ans[i], ans[j]; return cnt[a] > cnt[b] || cnt[a] == cnt[b] && a < b })
146148
return ans[:k]
147149
}
148150
```
149151

152+
#### TypeScript
153+
154+
```ts
155+
function topKFrequent(words: string[], k: number): string[] {
156+
const cnt: Map<string, number> = new Map();
157+
for (const w of words) {
158+
cnt.set(w, (cnt.get(w) || 0) + 1);
159+
}
160+
const ans: string[] = Array.from(cnt.keys());
161+
ans.sort((a, b) => {
162+
return cnt.get(a) === cnt.get(b) ? a.localeCompare(b) : cnt.get(b)! - cnt.get(a)!;
163+
});
164+
return ans.slice(0, k);
165+
}
166+
```
167+
150168
<!-- tabs:end -->
151169

152170
<!-- solution:end -->

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

+47-29
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ Note that &quot;i&quot; comes before &quot;love&quot; due to a lower alphabetica
6363

6464
<!-- solution:start -->
6565

66-
### Solution 1
66+
### Solution 1: Hash Table + Sorting
67+
68+
We can use a hash table $\textit{cnt}$ to record the frequency of each word. Then, we sort the key-value pairs in the hash table by value, and if the values are the same, we sort by key.
69+
70+
Finally, we take the first $k$ keys.
71+
72+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the number of words.
6773

6874
<!-- tabs:start -->
6975

@@ -82,23 +88,19 @@ class Solution:
8288
class Solution {
8389
public List<String> topKFrequent(String[] words, int k) {
8490
Map<String, Integer> cnt = new HashMap<>();
85-
for (String v : words) {
86-
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
91+
for (String w : words) {
92+
cnt.merge(w, 1, Integer::sum);
8793
}
88-
PriorityQueue<String> q = new PriorityQueue<>((a, b) -> {
89-
int d = cnt.get(a) - cnt.get(b);
90-
return d == 0 ? b.compareTo(a) : d;
94+
Arrays.sort(words, (a, b) -> {
95+
int c1 = cnt.get(a), c2 = cnt.get(b);
96+
return c1 == c2 ? a.compareTo(b) : c2 - c1;
9197
});
92-
for (String v : cnt.keySet()) {
93-
q.offer(v);
94-
if (q.size() > k) {
95-
q.poll();
98+
List<String> ans = new ArrayList<>();
99+
for (int i = 0; i < words.length && ans.size() < k; ++i) {
100+
if (i == 0 || !words[i].equals(words[i - 1])) {
101+
ans.add(words[i]);
96102
}
97103
}
98-
LinkedList<String> ans = new LinkedList<>();
99-
while (!q.isEmpty()) {
100-
ans.addFirst(q.poll());
101-
}
102104
return ans;
103105
}
104106
}
@@ -111,13 +113,17 @@ class Solution {
111113
public:
112114
vector<string> topKFrequent(vector<string>& words, int k) {
113115
unordered_map<string, int> cnt;
114-
for (auto& v : words) ++cnt[v];
116+
for (const auto& w : words) {
117+
++cnt[w];
118+
}
115119
vector<string> ans;
116-
for (auto& [key, _] : cnt) ans.emplace_back(key);
117-
sort(ans.begin(), ans.end(), [&](const string& a, const string& b) -> bool {
118-
return cnt[a] == cnt[b] ? a < b : cnt[a] > cnt[b];
120+
for (const auto& [w, _] : cnt) {
121+
ans.push_back(w);
122+
}
123+
ranges::sort(ans, [&](const string& a, const string& b) {
124+
return cnt[a] > cnt[b] || (cnt[a] == cnt[b] && a < b);
119125
});
120-
ans.erase(ans.begin() + k, ans.end());
126+
ans.resize(k);
121127
return ans;
122128
}
123129
};
@@ -126,23 +132,35 @@ public:
126132
#### Go
127133
128134
```go
129-
func topKFrequent(words []string, k int) []string {
135+
func topKFrequent(words []string, k int) (ans []string) {
130136
cnt := map[string]int{}
131-
for _, v := range words {
132-
cnt[v]++
137+
for _, w := range words {
138+
cnt[w]++
133139
}
134-
ans := []string{}
135-
for v := range cnt {
136-
ans = append(ans, v)
140+
for w := range cnt {
141+
ans = append(ans, w)
137142
}
138-
sort.Slice(ans, func(i, j int) bool {
139-
a, b := ans[i], ans[j]
140-
return cnt[a] > cnt[b] || cnt[a] == cnt[b] && a < b
141-
})
143+
sort.Slice(ans, func(i, j int) bool { a, b := ans[i], ans[j]; return cnt[a] > cnt[b] || cnt[a] == cnt[b] && a < b })
142144
return ans[:k]
143145
}
144146
```
145147

148+
#### TypeScript
149+
150+
```ts
151+
function topKFrequent(words: string[], k: number): string[] {
152+
const cnt: Map<string, number> = new Map();
153+
for (const w of words) {
154+
cnt.set(w, (cnt.get(w) || 0) + 1);
155+
}
156+
const ans: string[] = Array.from(cnt.keys());
157+
ans.sort((a, b) => {
158+
return cnt.get(a) === cnt.get(b) ? a.localeCompare(b) : cnt.get(b)! - cnt.get(a)!;
159+
});
160+
return ans.slice(0, k);
161+
}
162+
```
163+
146164
<!-- tabs:end -->
147165

148166
<!-- solution:end -->

solution/0600-0699/0692.Top K Frequent Words/Solution.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ class Solution {
22
public:
33
vector<string> topKFrequent(vector<string>& words, int k) {
44
unordered_map<string, int> cnt;
5-
for (auto& v : words) ++cnt[v];
5+
for (const auto& w : words) {
6+
++cnt[w];
7+
}
68
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];
9+
for (const auto& [w, _] : cnt) {
10+
ans.push_back(w);
11+
}
12+
ranges::sort(ans, [&](const string& a, const string& b) {
13+
return cnt[a] > cnt[b] || (cnt[a] == cnt[b] && a < b);
1014
});
11-
ans.erase(ans.begin() + k, ans.end());
15+
ans.resize(k);
1216
return ans;
1317
}
14-
};
18+
};
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
func topKFrequent(words []string, k int) []string {
1+
func topKFrequent(words []string, k int) (ans []string) {
22
cnt := map[string]int{}
3-
for _, v := range words {
4-
cnt[v]++
3+
for _, w := range words {
4+
cnt[w]++
55
}
6-
ans := []string{}
7-
for v := range cnt {
8-
ans = append(ans, v)
6+
for w := range cnt {
7+
ans = append(ans, w)
98
}
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-
})
9+
sort.Slice(ans, func(i, j int) bool { a, b := ans[i], ans[j]; return cnt[a] > cnt[b] || cnt[a] == cnt[b] && a < b })
1410
return ans[:k]
15-
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
class Solution {
22
public List<String> topKFrequent(String[] words, int k) {
33
Map<String, Integer> cnt = new HashMap<>();
4-
for (String v : words) {
5-
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
4+
for (String w : words) {
5+
cnt.merge(w, 1, Integer::sum);
66
}
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;
7+
Arrays.sort(words, (a, b) -> {
8+
int c1 = cnt.get(a), c2 = cnt.get(b);
9+
return c1 == c2 ? a.compareTo(b) : c2 - c1;
1010
});
11-
for (String v : cnt.keySet()) {
12-
q.offer(v);
13-
if (q.size() > k) {
14-
q.poll();
11+
List<String> ans = new ArrayList<>();
12+
for (int i = 0; i < words.length && ans.size() < k; ++i) {
13+
if (i == 0 || !words[i].equals(words[i - 1])) {
14+
ans.add(words[i]);
1515
}
1616
}
17-
LinkedList<String> ans = new LinkedList<>();
18-
while (!q.isEmpty()) {
19-
ans.addFirst(q.poll());
20-
}
2117
return ans;
2218
}
23-
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function topKFrequent(words: string[], k: number): string[] {
2+
const cnt: Map<string, number> = new Map();
3+
for (const w of words) {
4+
cnt.set(w, (cnt.get(w) || 0) + 1);
5+
}
6+
const ans: string[] = Array.from(cnt.keys());
7+
ans.sort((a, b) => {
8+
return cnt.get(a) === cnt.get(b) ? a.localeCompare(b) : cnt.get(b)! - cnt.get(a)!;
9+
});
10+
return ans.slice(0, k);
11+
}

0 commit comments

Comments
 (0)