-
Notifications
You must be signed in to change notification settings - Fork 0
/
347.top-k-frequent-elements.go
78 lines (60 loc) · 1.54 KB
/
347.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* @lc app=leetcode id=347 lang=golang
*
* [347] Top K Frequent Elements
*/
// @lc code=start
import "math/rand"
func topKFrequent(nums []int, k int) []int {
// quick select , TC: O(N)
countMap := make(map[int]int)
for _, v := range nums {
countMap[v]++
}
frequent := make([][]int, len(countMap))
for i := 0; i < len(countMap); i++ {
frequent[i] = make([]int, 2)
}
i := 0
for k, v := range countMap {
frequent[i][0] = k
frequent[i][1] = v
i++
}
quickSelect(&frequent, len(countMap)-k, 0, len(countMap)-1)
res := []int{}
for i := len(countMap) - k; i < len(countMap); i++ {
res = append(res, frequent[i][0])
}
return res
}
func quickSelect(arr *[][]int, kSmall, start, end int) {
if start == end {
return
}
// select pivot randomly
pivotIndex := start + rand.Intn(end-start)
selectedIndex := partition(arr, start, end, pivotIndex)
if selectedIndex > kSmall {
quickSelect(arr, kSmall, start, selectedIndex-1)
} else if selectedIndex < kSmall {
quickSelect(arr, kSmall, selectedIndex+1, end)
} else {
return
}
}
func partition(arr *[][]int, start, end, pivotIndex int) int {
pivotValue := (*arr)[pivotIndex][1]
// move pivot to end position
(*arr)[pivotIndex], (*arr)[end] = (*arr)[end], (*arr)[pivotIndex]
nextLeftIndex := start
for i := start; i < end; i++ {
if (*arr)[i][1] < pivotValue {
(*arr)[i], (*arr)[nextLeftIndex] = (*arr)[nextLeftIndex], (*arr)[i]
nextLeftIndex++
}
}
(*arr)[nextLeftIndex], (*arr)[end] = (*arr)[end], (*arr)[nextLeftIndex]
return nextLeftIndex
}
// @lc code=end