You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0600-0699/0692.Top K Frequent Words/README_EN.md
+47-29
Original file line number
Diff line number
Diff line change
@@ -63,7 +63,13 @@ Note that "i" comes before "love" due to a lower alphabetica
63
63
64
64
<!-- solution:start -->
65
65
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.
0 commit comments