-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtop_k_frequent_elements.go
56 lines (45 loc) · 1.05 KB
/
top_k_frequent_elements.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
package main
import "container/heap"
type Word struct {
word int
frequency int
}
func newWord(word int, frequency int) *Word {
return &Word{word: word, frequency: frequency}
}
type H struct {
items []*Word
}
func (h *H) Len() int { return len(h.items) }
func (h *H) Less(i, j int) bool {
if h.items[i].frequency != h.items[j].frequency {
return h.items[i].frequency < h.items[j].frequency
}
return h.items[i].word > h.items[j].word
}
func (h *H) Swap(i, j int) { h.items[i], h.items[j] = h.items[j], h.items[i] }
func (h *H) Push(i interface{}) { h.items = append(h.items, i.(*Word)) }
func (h *H) Pop() interface{} {
n := len(h.items)
item := h.items[n-1]
h.items = h.items[:n-1]
return item
}
func topKFrequent(words []int, k int) []int {
counts := map[int]int{}
h := &H{}
for _, v := range words {
counts[v]++
}
for key, v := range counts {
heap.Push(h, newWord(key, v))
if h.Len() > k {
heap.Pop(h)
}
}
result := []int{}
for h.Len() > 0 {
result = append([]int{heap.Pop(h).(*Word).word}, result...)
}
return result
}