forked from christine-1017/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopKFrequentWords.java
More file actions
33 lines (32 loc) · 1.12 KB
/
TopKFrequentWords.java
File metadata and controls
33 lines (32 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class TopKFrequentWords {
public String[] topKFrequent(String[] combo, int k) {
if (k <= 0 || combo.length == 0) {
return new String[0];
}
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < combo.length; i++) {
if (map.containsKey(combo[i])) {
map.put(combo[i], map.get(combo[i]) + 1);
} else {
map.put(combo[i], 1);
}
}
PriorityQueue<Map.Entry<String, Integer>> queue = new PriorityQueue<Map.Entry<String, Integer>>(combo.length, new Comparator<Map.Entry<String, Integer>>(){
@Override
public int compare(Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2) {
if (e1.getValue() == e2.getValue()) {
return 0;
}
return e1.getValue() < e2.getValue() ? 1 : -1;
}
});
for (Map.Entry<String, Integer> entry: map.entrySet()) {
queue.offer(entry);
}
String[] res = new String[Math.min(Math.min(k, combo.length), map.size())];
for (int i = 0; i < k && i < combo.length && i < map.size(); i++) {
res[i] = queue.poll().getKey();
}
return res;
}
}