-
Notifications
You must be signed in to change notification settings - Fork 522
/
c.go
45 lines (41 loc) · 897 Bytes
/
c.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
package main
import "sort"
func bfsWithDepth(g [][]int, st int, do func(v, dep int)) {
visited := make([]bool, len(g))
visited[st] = true
type pair struct{ v, dep int }
queue := []pair{{st, 0}}
for len(queue) > 0 {
var p pair
p, queue = queue[0], queue[1:]
do(p.v, p.dep)
for _, w := range g[p.v] {
if !visited[w] {
visited[w] = true
queue = append(queue, pair{w, p.dep + 1})
}
}
}
}
func watchedVideosByFriends(watchedVideos [][]string, g [][]int, st int, level int) []string {
cntMap := map[string]int{}
bfsWithDepth(g, st, func(v, dep int) {
if dep == level {
for _, video := range watchedVideos[v] {
cntMap[video]++
}
}
})
cnt := make([][]string, 200)
for k, v := range cntMap {
cnt[v] = append(cnt[v], k)
}
ans := []string{}
for _, c := range cnt {
if len(c) > 0 {
sort.Strings(c)
ans = append(ans, c...)
}
}
return ans
}