-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.go
64 lines (52 loc) · 1.07 KB
/
solution.go
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
package top_k_frequent_words
import (
"container/heap"
)
type wordCounts []wordCount
type wordCount struct {
word string
count int
}
func (wcs wordCounts) Len() int {
return len(wcs)
}
func (wcs wordCounts) Less(i, j int) bool {
if wcs[i].count != wcs[j].count {
return wcs[i].count < wcs[j].count
} else {
return wcs[i].word > wcs[j].word
}
}
func (wcs wordCounts) Swap(i, j int) {
wcs[i], wcs[j] = wcs[j], wcs[i]
}
func (wcs *wordCounts) Push(x interface{}) {
*wcs = append(*wcs, x.(wordCount))
}
func (wcs *wordCounts) Pop() interface{} {
target := (*wcs)[len(*wcs)-1]
*wcs = (*wcs)[:len(*wcs)-1]
return target
}
func topKFrequent(words []string, k int) []string {
counter := map[string]int{}
for _, word := range words {
counter[word] += 1
}
var wcs wordCounts
heap.Init(&wcs)
for word, count := range counter {
heap.Push(&wcs, wordCount{
word: word,
count: count,
})
if wcs.Len() > k {
heap.Pop(&wcs)
}
}
result := make([]string, k)
for i := k - 1; i >= 0; i-- {
result[i] = heap.Pop(&wcs).(wordCount).word
}
return result
}