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

本文涉及知识点

C++BFS算法

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

有 n 个人,每个人都有一个 0 到 n-1 的唯一 id 。
给你数组 watchedVideos 和 friends ,其中 watchedVideos[i] 和 friends[i] 分别表示 id = i 的人观看过的视频列表和他的好友列表。
Level 1 的视频包含所有你好友观看过的视频,level 2 的视频包含所有你好友的好友观看过的视频,以此类推。一般的,Level 为 k 的视频包含所有从你出发,最短距离为 k 的好友观看过的视频。
给定你的 id 和一个 level 值,请你找出所有指定 level 的视频,并将它们按观看频率升序返回。如果有频率相同的视频,请将它们按字母顺序从小到大排列。
示例 1:

在这里插入图片描述

输入:watchedVideos = [[“A”,“B”],[“C”],[“B”,“C”],[“D”]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
输出:[“B”,“C”]
解释:
你的 id 为 0(绿色),你的朋友包括(黄色):
id 为 1 -> watchedVideos = [“C”]
id 为 2 -> watchedVideos = [“B”,“C”]
你朋友观看过视频的频率为:
B -> 1
C -> 2
示例 2:
在这里插入图片描述

输入:watchedVideos = [[“A”,“B”],[“C”],[“B”,“C”],[“D”]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
输出:[“D”]
解释:
你的 id 为 0(绿色),你朋友的朋友只有一个人,他的 id 为 3(黄色)。

提示:

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
如果 friends[i] 包含 j ,那么 friends[j] 包含 i

题解

一,通过BFS求出第leve 层朋友。
二,利用哈希映射mVideoCnt记录leve层朋友看的视频及次数。
三,有序映射sCntVideo记录:观看次数、视频名称。
四,按sCntVideo顺序返回视频名称。
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);
		}

扩展阅读

我想对大家说的话
工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。
学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作
有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注
闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。
如果程序是一条龙,那算法就是他的是睛
失败+反思=成功 成功+反思=成功

视频课程

先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176

测试环境

操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。

  • 21
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
好的,以下是一个基于C++BFS求最短路径的示例代码: ```cpp #include <iostream> #include <queue> #include <cstring> using namespace std; const int MAXN = 1005; // 最大节点数 const int INF = 0x3f3f3f3f; // 无穷大 int n, m; // n表示节点个数,m表示边数 int dis[MAXN]; // 存储起点到每个节点的距离 bool vis[MAXN]; // 记录每个节点是否被访问过 int head[MAXN], to[MAXN<<1], nxt[MAXN<<1], edge[MAXN<<1], cnt; // 邻接表存图 void addEdge(int u, int v, int w) { to[++cnt] = v; nxt[cnt] = head[u]; edge[cnt] = w; head[u] = cnt; } void bfs(int start) { memset(dis, INF, sizeof(dis)); // 初始化距离为无穷大 memset(vis, false, sizeof(vis)); // 初始化所有节点都未被访问过 dis[start] = 0; // 起点到自己的距离为0 vis[start] = true; // 起点已经访问过了 queue<int> q; // 定义一个队列,用于广度优先搜索 q.push(start); // 将起点加入队列 while (!q.empty()) { int u = q.front(); q.pop(); // 取出队首节点 for (int i = head[u]; i; i = nxt[i]) { int v = to[i], w = edge[i]; if (!vis[v]) { // 如果这个节点未被访问过 dis[v] = dis[u] + w; // 更新距离 vis[v] = true; // 标记为已访问 q.push(v); // 将其加入队列 } } } } int main() { cin >> n >> m; for (int i = 1; i <= m; ++i) { int u, v, w; cin >> u >> v >> w; addEdge(u, v, w); addEdge(v, u, w); // 无向图需要加两条边 } int start; cin >> start; bfs(start); for (int i = 1; i <= n; ++i) { if (dis[i] != INF) { cout << start << " 到 " << i << " 的最短距离为:" << dis[i] << endl; } } return 0; } ``` 这段代码使用邻接表存图,时间复杂度为O(n+m),其中n表示节点数,m表示边数。在实际使用中,可以根据自己的需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闻缺陷则喜何志丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值