forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1311.go
46 lines (40 loc) · 1.04 KB
/
1311.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
var cnt map[string]int
func watchedVideosByFriends(watchedVideos [][]string, friends [][]int, id int, level int) []string {
seen := make([]int, len(friends))
q := []int{id}
seen[id] = 1
for level > 0 {
for i := len(q); i > 0; i-- {
idx := q[0]
q = q[1:]
for _, p := range friends[idx] {
if seen[p] == 0 {
seen[p] = 1
q = append(q, p)
}
}
}
level--
}
cnt = make(map[string]int)
for _, v := range q {
for _, s := range watchedVideos[v] {
cnt[s]++
}
}
res := []string{}
for k := range cnt {
res = append(res, k)
}
sort.Sort(strSlice(res))
return res
}
type strSlice []string
func (x strSlice) Len() int { return len(x) }
func (x strSlice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x strSlice) Less(i, j int) bool {
if cnt[x[i]] == cnt[x[j]] {
return x[i] < x[j]
}
return cnt[x[i]] < cnt[x[j]]
}