diff --git a/leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/1.png b/leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/1.png new file mode 100644 index 000000000..c9c7c97b4 Binary files /dev/null and b/leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/1.png differ diff --git a/leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/2.png b/leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/2.png new file mode 100644 index 000000000..6d5d6cab2 Binary files /dev/null and b/leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/2.png differ diff --git a/leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/README.md b/leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/README.md index 181b5124b..1f1748580 100644 --- a/leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/README.md +++ b/leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/README.md @@ -1,28 +1,36 @@ # [1311.Get Watched Videos by Your Friends][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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`. + +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. + +**Example 1:** -**Example 1:** +![1](./1.png) ``` -Input: a = "11", b = "1" -Output: "100" +Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1 +Output: ["B","C"] +Explanation: +You have id = 0 (green color in the figure) and your friends are (yellow color in the figure): +Person with id = 1 -> watchedVideos = ["C"] +Person with id = 2 -> watchedVideos = ["B","C"] +The frequencies of watchedVideos by your friends are: +B -> 1 +C -> 2 ``` -## 题意 -> ... +**Example 2:** -## 题解 +![2](./2.png) -### 思路1 -> ... -Get Watched Videos by Your Friends -```go ``` - +Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2 +Output: ["D"] +Explanation: +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). +``` ## 结语 diff --git a/leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/Solution.go b/leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/Solution.go index d115ccf5e..233cc66da 100644 --- a/leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/Solution.go +++ b/leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/Solution.go @@ -1,5 +1,42 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(watchedVideos [][]string, friends [][]int, id int, level int) []string { + now := 0 + var ans []string + queue := []int{id} + visited := [100]bool{} + visited[id] = true + for ; now < level; now++ { + nq := make([]int, 0) + videos := map[string]int{} + next := make([]string, 0) + for _, u := range queue { + for _, f := range friends[u] { + if visited[f] { + continue + } + visited[f] = true + nq = append(nq, f) + for _, w := range watchedVideos[f] { + videos[w]++ + } + } + } + for v := range videos { + next = append(next, v) + } + sort.Slice(next, func(i, j int) bool { + a, b := videos[next[i]], videos[next[j]] + if a == b { + return next[i] < next[j] + } + return a < b + }) + ans = next + queue = nq + } + return ans + } diff --git a/leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/Solution_test.go b/leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/Solution_test.go index 14ff50eb4..f9d274d79 100644 --- a/leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/Solution_test.go +++ b/leetcode/1301-1400/1311.Get-Watched-Videos-by-Your-Friends/Solution_test.go @@ -9,31 +9,32 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + watchedVideos [][]string + friends [][]int + id, level int + expect []string }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]string{{"A", "B"}, {"C"}, {"B", "C"}, {"D"}}, [][]int{{1, 2}, {0, 3}, {0, 3}, {1, 2}}, 0, 1, []string{"B", "C"}}, + {"TestCase2", [][]string{{"A", "B"}, {"C"}, {"B", "C"}, {"D"}}, [][]int{{1, 2}, {0, 3}, {0, 3}, {1, 2}}, 0, 2, []string{"D"}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.watchedVideos, c.friends, c.id, c.level) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v", + c.expect, got, c.watchedVideos, c.friends, c.id, c.level) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }