Skip to content

Commit 9c94dc5

Browse files
committed
solve problem Top K Frequent Words
1 parent 660ed84 commit 9c94dc5

File tree

5 files changed

+202
-0
lines changed

5 files changed

+202
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ All solutions will be accepted!
316316
|451|[Sort Characters By Frequency](https://leetcode-cn.com/problems/sort-characters-by-frequency/description/)|[java/py/js](./algorithms/SortCharactersByFrequency)|Medium|
317317
|215|[Kth Largest Element In An Array](https://leetcode-cn.com/problems/kth-largest-element-in-an-array/description/)|[java/py/js](./algorithms/KthLargestElementInAnArray)|Medium|
318318
|347|[Top K Frequent Elements](https://leetcode-cn.com/problems/top-k-frequent-elements/description/)|[java/py/js](./algorithms/TopKFrequentElements)|Medium|
319+
|692|[Top K Frequent Words](https://leetcode-cn.com/problems/top-k-frequent-words/description/)|[java/py/js](./algorithms/TopKFrequentWords)|Medium|
319320

320321
# Database
321322
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Top K Frequent Words
2+
We can solve this problem by max heap and hashmap, like [Top K Frequent Elements](./TopKFrequentElements)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
class Frequency {
3+
String word;
4+
int frequency;
5+
6+
Frequency(String word, int frequency) {
7+
this.word = word;
8+
this.frequency = frequency;
9+
}
10+
}
11+
12+
public List<String> topKFrequent(String[] words, int k) {
13+
Map<String, Integer> frequencyMap = new HashMap<String, Integer>();
14+
for (String word : words) {
15+
if (frequencyMap.get(word) == null)
16+
frequencyMap.put(word, 1);
17+
else
18+
frequencyMap.put(word, frequencyMap.get(word) + 1);
19+
}
20+
21+
Comparator<Frequency> frequencyComparator = new Comparator<Frequency>() {
22+
public int compare(Frequency a, Frequency b) {
23+
return a.frequency > b.frequency ? -1 : (a.frequency == b.frequency ? a.word.compareTo(b.word) : 1);
24+
}
25+
};
26+
Queue<Frequency> queue = new PriorityQueue<Frequency>(frequencyComparator);
27+
for (String word : frequencyMap.keySet()) {
28+
queue.add(new Frequency(word, frequencyMap.get(word)));
29+
}
30+
31+
List<String> res = new ArrayList<String>();
32+
for (int i = 0; i < k; i++)
33+
res.add(queue.poll().word);
34+
return res;
35+
}
36+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* @param {string[]} words
3+
* @param {number} k
4+
* @return {string[]}
5+
*/
6+
var topKFrequent = function(words, k) {
7+
let frequencyMap = new Map()
8+
words.forEach(word => {
9+
if (!frequencyMap.has(word))
10+
frequencyMap.set(word, 1)
11+
else
12+
frequencyMap.set(word, frequencyMap.get(word) + 1)
13+
})
14+
15+
let maxFrequencyHeap = new MaxFrequencyHeap()
16+
for (let [word, frequency] of frequencyMap.entries())
17+
maxFrequencyHeap.push(word, frequency)
18+
19+
let res = []
20+
for (let i = 0; i < k; i++)
21+
res.push(maxFrequencyHeap.pop())
22+
return res
23+
};
24+
25+
function MaxFrequencyHeap() {
26+
this.heap = []
27+
};
28+
29+
MaxFrequencyHeap.prototype.push = function(word, frequency) {
30+
this.heap.push({word, frequency})
31+
let pos = this.heap.length - 1,
32+
pPos = (pos % 2 == 0 ? pos - 2 : pos - 1) / 2
33+
34+
while (pPos >= 0 && (frequency > this.heap[pPos].frequency ||
35+
(frequency == this.heap[pPos].frequency &&
36+
word < this.heap[pPos].word))) {
37+
let temp = this.heap[pos]
38+
this.heap[pos] = this.heap[pPos]
39+
this.heap[pPos] = temp
40+
pos = pPos
41+
pPos = (pos % 2 == 0 ? pos - 2 : pos - 1) / 2
42+
}
43+
};
44+
45+
MaxFrequencyHeap.prototype.size = function() {
46+
return this.heap.length
47+
};
48+
49+
MaxFrequencyHeap.prototype.pop = function() {
50+
let size = this.size()
51+
if (size == 0)
52+
throw new Error('pop form empty list')
53+
else if (size == 1)
54+
return this.heap.pop().word
55+
56+
let word = this.heap[0].word,
57+
pos = 0,
58+
maxChildPos = pos * 2 + 2
59+
this.heap[0] = this.heap.pop()
60+
61+
if (maxChildPos < size - 1) {
62+
if (this.heap[maxChildPos].frequency < this.heap[maxChildPos - 1].frequency ||
63+
(this.heap[maxChildPos].frequency == this.heap[maxChildPos - 1].frequency &&
64+
this.heap[maxChildPos].word > this.heap[maxChildPos - 1].word))
65+
maxChildPos = maxChildPos - 1
66+
} else
67+
maxChildPos = maxChildPos - 1 < size - 1 ? maxChildPos - 1 : pos
68+
69+
while (maxChildPos < size - 1 && (this.heap[pos].frequency < this.heap[maxChildPos].frequency ||
70+
(this.heap[pos].frequency == this.heap[maxChildPos].frequency &&
71+
this.heap[pos].word > this.heap[maxChildPos].word))) {
72+
let temp = this.heap[maxChildPos]
73+
this.heap[maxChildPos] = this.heap[pos]
74+
this.heap[pos] = temp
75+
pos = maxChildPos
76+
maxChildPos = pos * 2 + 2
77+
78+
if (maxChildPos < size - 1) {
79+
if (this.heap[maxChildPos].frequency < this.heap[maxChildPos - 1].frequency ||
80+
(this.heap[maxChildPos].frequency == this.heap[maxChildPos - 1].frequency &&
81+
this.heap[maxChildPos].word > this.heap[maxChildPos - 1].word))
82+
maxChildPos = maxChildPos - 1
83+
} else
84+
maxChildPos = maxChildPos - 1 < size - 1 ? maxChildPos - 1 : pos
85+
}
86+
87+
return word
88+
};
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
class Solution(object):
2+
class MaxFrequencyHeap(object):
3+
def __init__(self):
4+
self.heap = []
5+
6+
def push(self, word, frequency):
7+
self.heap.append({ 'word': word, 'frequency': frequency})
8+
pos = self.size() - 1
9+
p_pos = (pos - 2 if pos % 2 == 0 else pos - 1) / 2
10+
11+
while p_pos >= 0 and (frequency > self.heap[p_pos]['frequency'] or \
12+
(frequency == self.heap[p_pos]['frequency'] and word < self.heap[p_pos]['word'])):
13+
self.heap[pos], self.heap[p_pos] = self.heap[p_pos], self.heap[pos]
14+
pos = p_pos
15+
p_pos = (pos - 2 if pos % 2 == 0 else pos - 1) / 2
16+
17+
def size(self):
18+
return len(self.heap)
19+
20+
def pop(self):
21+
size = self.size()
22+
if size == 0:
23+
raise IndexError('pop form empty list')
24+
elif size == 1:
25+
return self.heap.pop()['word']
26+
27+
word = self.heap[0]['word']
28+
self.heap[0] = self.heap.pop()
29+
pos = 0
30+
max_child_pos = pos * 2 + 2
31+
if max_child_pos < size - 1:
32+
if self.heap[max_child_pos]['frequency'] < self.heap[max_child_pos - 1]['frequency'] \
33+
or (self.heap[max_child_pos]['frequency'] == self.heap[max_child_pos - 1]['frequency'] and \
34+
self.heap[max_child_pos]['word'] > self.heap[max_child_pos - 1]['word']):
35+
max_child_pos = max_child_pos - 1
36+
else:
37+
max_child_pos = max_child_pos - 1 if max_child_pos - 1 < size - 1 else pos
38+
39+
while max_child_pos < size - 1 and (self.heap[pos]['frequency'] < self.heap[max_child_pos]['frequency'] or \
40+
(self.heap[pos]['frequency'] == self.heap[max_child_pos]['frequency'] and \
41+
self.heap[pos]['word'] > self.heap[max_child_pos]['word'])):
42+
self.heap[pos], self.heap[max_child_pos] = self.heap[max_child_pos], self.heap[pos]
43+
pos = max_child_pos
44+
max_child_pos = pos * 2 + 2
45+
if max_child_pos < size - 1:
46+
if self.heap[max_child_pos]['frequency'] < self.heap[max_child_pos - 1]['frequency'] \
47+
or (self.heap[max_child_pos]['frequency'] == self.heap[max_child_pos - 1]['frequency'] and \
48+
self.heap[max_child_pos]['word'] > self.heap[max_child_pos - 1]['word']):
49+
max_child_pos = max_child_pos - 1
50+
else:
51+
max_child_pos = max_child_pos - 1 if max_child_pos - 1 < size - 1 else pos
52+
53+
return word
54+
55+
def topKFrequent(self, words, k):
56+
"""
57+
:type words: List[str]
58+
:type k: int
59+
:rtype: List[str]
60+
"""
61+
frequency_map = {}
62+
for word in words:
63+
if not frequency_map.get(word):
64+
frequency_map[word] = 1
65+
else:
66+
frequency_map[word] += 1
67+
68+
max_frequency_heap = self.MaxFrequencyHeap()
69+
for word, frequency in frequency_map.items():
70+
max_frequency_heap.push(word, frequency)
71+
72+
res = []
73+
for i in xrange(k):
74+
res.append(max_frequency_heap.pop())
75+
return res

0 commit comments

Comments
 (0)