Skip to content

Commit 39fe6fa

Browse files
authored
Merge pull request #1238 from 0xff-dev/1311
Add solution and test-cases for problem 1311
2 parents dee484c + 2830098 commit 39fe6fa

File tree

5 files changed

+73
-27
lines changed

5 files changed

+73
-27
lines changed
Loading
Loading

leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# [1311.Get Watched Videos by Your Friends][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There are `n` people, each person has a unique id between `0` and `n-1`. Given the arrays `watchedVideos` and `friends`, where `watchedVideos[i]` and `friends[i]` contain the list of watched videos and the list of friends respectively for the person with `id = i`.
5+
6+
Level **1** of videos are all watched videos by your friends, level **2** of videos are all watched videos by the friends of your friends and so on. In general, the level `k` of videos are all watched videos by people with the shortest path **exactly** equal to `k` with you. Given your `id` and the `level` of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
7+
8+
**Example 1:**
79

8-
**Example 1:**
10+
![1](./1.png)
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
14+
Output: ["B","C"]
15+
Explanation:
16+
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
17+
Person with id = 1 -> watchedVideos = ["C"]
18+
Person with id = 2 -> watchedVideos = ["B","C"]
19+
The frequencies of watchedVideos by your friends are:
20+
B -> 1
21+
C -> 2
1322
```
1423

15-
## 题意
16-
> ...
24+
**Example 2:**
1725

18-
## 题解
26+
![2](./2.png)
1927

20-
### 思路1
21-
> ...
22-
Get Watched Videos by Your Friends
23-
```go
2428
```
25-
29+
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
30+
Output: ["D"]
31+
Explanation:
32+
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
33+
```
2634

2735
## 结语
2836

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(watchedVideos [][]string, friends [][]int, id int, level int) []string {
6+
now := 0
7+
var ans []string
8+
queue := []int{id}
9+
visited := [100]bool{}
10+
visited[id] = true
11+
for ; now < level; now++ {
12+
nq := make([]int, 0)
13+
videos := map[string]int{}
14+
next := make([]string, 0)
15+
for _, u := range queue {
16+
for _, f := range friends[u] {
17+
if visited[f] {
18+
continue
19+
}
20+
visited[f] = true
21+
nq = append(nq, f)
22+
for _, w := range watchedVideos[f] {
23+
videos[w]++
24+
}
25+
}
26+
}
27+
for v := range videos {
28+
next = append(next, v)
29+
}
30+
sort.Slice(next, func(i, j int) bool {
31+
a, b := videos[next[i]], videos[next[j]]
32+
if a == b {
33+
return next[i] < next[j]
34+
}
35+
return a < b
36+
})
37+
ans = next
38+
queue = nq
39+
}
40+
return ans
41+
542
}

leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/Solution_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
watchedVideos [][]string
14+
friends [][]int
15+
id, level int
16+
expect []string
1517
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
18+
{"TestCase1", [][]string{{"A", "B"}, {"C"}, {"B", "C"}, {"D"}}, [][]int{{1, 2}, {0, 3}, {0, 3}, {1, 2}}, 0, 1, []string{"B", "C"}},
19+
{"TestCase2", [][]string{{"A", "B"}, {"C"}, {"B", "C"}, {"D"}}, [][]int{{1, 2}, {0, 3}, {0, 3}, {1, 2}}, 0, 2, []string{"D"}},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.watchedVideos, c.friends, c.id, c.level)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v",
28+
c.expect, got, c.watchedVideos, c.friends, c.id, c.level)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)