Skip to content

Commit 5de18a6

Browse files
refactor 692
1 parent 8c2a8ff commit 5de18a6

File tree

1 file changed

+1
-29
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+1
-29
lines changed

src/main/java/com/fishercoder/solutions/_692.java

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,13 @@
88
import java.util.SortedSet;
99
import java.util.TreeSet;
1010

11-
/**
12-
* 692. Top K Frequent Words
13-
*
14-
* Given a non-empty list of words, return the k most frequent elements.
15-
* Your answer should be sorted by frequency from highest to lowest.
16-
* If two words have the same frequency, then the word with the lower alphabetical order comes first.
17-
18-
Example 1:
19-
Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
20-
Output: ["i", "love"]
21-
Explanation: "i" and "love" are the two most frequent words.
22-
Note that "i" comes before "love" due to a lower alphabetical order.
23-
24-
Example 2:
25-
Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
26-
Output: ["the", "is", "sunny", "day"]
27-
Explanation: "the", "is", "sunny" and "day" are the four most frequent words,
28-
with the number of occurrence being 4, 3, 2 and 1 respectively.
29-
30-
Note:
31-
You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
32-
Input words contain only lowercase letters.
33-
34-
Follow up:
35-
Try to solve it in O(n log k) time and O(n) extra space.
36-
Can you solve it in O(n) time with only O(k) extra space?
37-
38-
*/
3911
public class _692 {
4012

4113
public static class Solution1 {
4214
/**
4315
* O(n) extra space
4416
* O(nlogk) time
45-
* */
17+
*/
4618
public List<String> topKFrequent(String[] words, int k) {
4719
Map<String, Integer> map = new HashMap<>();
4820
for (String word : words) {

0 commit comments

Comments
 (0)