# 【C++BFS】1311. 获取你好友已观看的视频

22 篇文章 0 订阅

C++BFS算法

## LeetCode1311. 获取你好友已观看的视频

Level 1 的视频包含所有你好友观看过的视频，level 2 的视频包含所有你好友的好友观看过的视频，以此类推。一般的，Level 为 k 的视频包含所有从你出发，最短距离为 k 的好友观看过的视频。

id 为 1 -> watchedVideos = [“C”]
id 为 2 -> watchedVideos = [“B”,“C”]

B -> 1
C -> 2

n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n

## 题解

BFS的状态表示：leves[0] = {id}，leves[i]记录leves[i-1]的直接朋友。
BFS的状态表示：通过next枚举cur的直接朋友。
BFS的初始状态：leves[0] = {id}
BFS的返回值：leves[leve]
BFS的出重处理：数组vis出重。

## 代码

### 核心代码

class Solution {
public:
vector<string> watchedVideosByFriends(vector<vector<string>>& watchedVideos, vector<vector<int>>& friends, int id, int level) {
const int N = watchedVideos.size();
vector<vector<int>> leves = { {id} };
vector<bool> vis(N);
vis[id] = true;
for (int i = 0; i < leves.size(); i++) {
vector<int> nexts;
for (const auto& cur : leves[i]) {
for (const auto& next : friends[cur]) {
if (vis[next]) { continue; }
vis[next] = true;
nexts.emplace_back(next);
}
}
if (nexts.empty()) { break; }
leves.emplace_back(nexts);
}
if (level >= leves.size()) { return {}; };
unordered_map<string, int> mVideoCnt;
for (const auto& i : leves[level]) {
for (const auto& s : watchedVideos[i]) {
mVideoCnt[s]++;
}
}
set<pair<int, string>> sCntVideo;
for (const auto& [s, cnt] : mVideoCnt) {
sCntVideo.emplace(cnt, s);
}
vector<string> ret;
for (const auto& [tmp, s] : sCntVideo) {
ret.emplace_back(s);
}
return ret;
}
};


### 单元测试

vector<vector<string>> watchedVideos;
vector<vector<int>> friends;
int id, level;
TEST_METHOD(TestMethod1)
{
watchedVideos = { {"A","B"},{"C"},{"B","C"},{"D"} }, friends = { {1,2,3},{},{},{} }, id = 0, level = 2;
auto res = Solution().watchedVideosByFriends(watchedVideos, friends, id, level);
AssertEx(vector<string>{}, res);
}
TEST_METHOD(TestMethod2)
{
watchedVideos = { {"A"},{"B"},{"C"},{"D"} }, friends = { {1},{2},{3},{} }, id = 0, level = 2;
auto res = Solution().watchedVideosByFriends(watchedVideos, friends, id, level);
AssertEx(vector<string>{"C"}, res);
}
TEST_METHOD(TestMethod3)
{
watchedVideos = { {"A","B"},{"A"},{"B"},{"A"} }, friends = { {1,2,3},{},{},{} }, id = 0, level = 1;
auto res = Solution().watchedVideosByFriends(watchedVideos, friends, id, level);
AssertEx(vector<string>{"B","A"}, res);
}
TEST_METHOD(TestMethod4)
{
watchedVideos = { {"A","B"},{"A"},{"B"},{"B"} }, friends = { {1,2,3},{},{},{} }, id = 0, level = 1;
auto res = Solution().watchedVideosByFriends(watchedVideos, friends, id, level);
AssertEx(vector<string>{"A", "B"}, res);
}
TEST_METHOD(TestMethod5)
{
watchedVideos = { {"A","B"},{"A"},{"B"},{"C"} }, friends = { {1,2,3},{},{},{} }, id = 0, level = 1;
auto res = Solution().watchedVideosByFriends(watchedVideos, friends, id, level);
AssertEx(vector<string>{"A", "B","C"}, res);
}
TEST_METHOD(TestMethod6)
{
watchedVideos = { {"A","B"},{"A"},{"C"},{"B"} }, friends = { {1,2,3},{},{},{} }, id = 0, level = 1;
auto res = Solution().watchedVideosByFriends(watchedVideos, friends, id, level);
AssertEx(vector<string>{"A", "B", "C"}, res);
}
TEST_METHOD(TestMethod11)
{
watchedVideos = { {"A","B"},{"C"},{"B","C"},{"D"} }, friends = { {1,2},{0,3},{0,3},{1,2} }, id = 0, level = 1;
auto res = Solution().watchedVideosByFriends(watchedVideos, friends, id, level);
AssertEx(vector<string>{"B", "C"}, res);
}
TEST_METHOD(TestMethod12)
{
watchedVideos = { {"A","B"},{"C"},{"B","C"},{"D"} }, friends = { {1,2},{0,3},{0,3},{1,2} }, id = 0, level = 2;
auto res = Solution().watchedVideosByFriends(watchedVideos, friends, id, level);
AssertEx(vector<string>{"D"}, res);
}


## 扩展阅读

### 视频课程

https://edu.csdn.net/course/detail/38771

https://edu.csdn.net/lecturer/6176

### 测试环境

• 138
点赞
• 118
收藏
觉得还不错? 一键收藏
• 打赏
• 154
评论
03-09 1464
03-26
11-23 204
07-19 817
01-04 849
08-04 410
08-04 177
08-04 182

¥1 ¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。