-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
98 lines (96 loc) · 3.32 KB
/
Main.java
File metadata and controls
98 lines (96 loc) · 3.32 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.util.*;
// https://www.youtube.com/watch?v=POERw4yDVBw
public class TopKFrequentWords692 {
class Solution {
public List<String> topKFrequent(String[] words, int k) {
HashMap<String, Integer> map = new HashMap<>();
for (String word : words) {
int freq = map.getOrDefault(word, 0);
map.put(word, freq + 1);
}
Item[] items = new Item[map.size()];
int idx = 0;
for (Map.Entry<String, Integer> entry : map.entrySet()) {
items[idx++] = new Item(entry.getKey(), entry.getValue());
}
Arrays.sort(items, (x, y) -> {
if (x.freq == y.freq) {
return x.word.compareTo(y.word);
}
return - x.freq + y.freq;
});
LinkedList<String> list = new LinkedList<>();
for (int j = 0; j < k; j++) {
list.add(items[j].word);
}
return list;
}
class Item {
String word;
int freq;
public Item (String w, int f) {
word = w; freq = f;
}
}
}
class Solution2 {
public List<String> topKFrequent(String[] words, int k) {
HashMap<String, Integer> map = new HashMap<>();
for (String w : words) {
int freq = map.getOrDefault(w, 0);
freq++;
map.put(w, freq);
}
PriorityQueue<String> pq = new PriorityQueue<>(k, (x, y) -> {
int freqX = map.get(x);
int freqY = map.get(y);
if (freqX == freqY) {
return y.compareTo(x);
}
return freqX - freqY;
});
for (Map.Entry<String, Integer> entry : map.entrySet()) {
int size = pq.size();
if (size < k) {
pq.offer(entry.getKey());
} else {
// IMPORTANT: do in sequence
pq.offer(entry.getKey());
pq.poll();
}
}
LinkedList<String> list = new LinkedList<>();
while (!pq.isEmpty()) {
list.addFirst(pq.poll());
}
return list;
}
}
class Solution3 {
public List<String> topKFrequent(String[] words, int k) {
HashMap<String, Integer> map = new HashMap<>();
for (String w : words) {
int freq = map.getOrDefault(w, 0);
freq++;
map.put(w, freq);
}
PriorityQueue<Map.Entry<String, Integer>> pq = new PriorityQueue<>(k, (x, y) -> {
if (x.getValue().equals(y.getValue())) {
return -x.getKey().compareTo(y.getKey());
}
return x.getValue() - y.getValue();
});
for (Map.Entry<String, Integer> entry : map.entrySet()) {
pq.offer(entry);
if (pq.size() > k) {
pq.poll();
}
}
LinkedList<String> list = new LinkedList<>();
while (!pq.isEmpty()) {
list.addFirst(pq.poll().getKey());
}
return list;
}
}
}