|
8 | 8 | import java.util.SortedSet;
|
9 | 9 | import java.util.TreeSet;
|
10 | 10 |
|
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 |
| - */ |
39 | 11 | public class _692 {
|
40 | 12 |
|
41 | 13 | public static class Solution1 {
|
42 | 14 | /**
|
43 | 15 | * O(n) extra space
|
44 | 16 | * O(nlogk) time
|
45 |
| - * */ |
| 17 | + */ |
46 | 18 | public List<String> topKFrequent(String[] words, int k) {
|
47 | 19 | Map<String, Integer> map = new HashMap<>();
|
48 | 20 | for (String word : words) {
|
|
0 commit comments