阿里巴巴专场——第322场周赛题解
创始人
2024-04-17 07:53:12
0

 

目录

模拟法:6253.回环句

排序后模拟:6254. 划分技能点相等的团队

BFS:6255. 两个城市间路径的最小分数

BFS:6256. 将节点分成尽可能多的组


模拟法:6253.回环句

 

这道题直接按照题目的意思暴力模拟即可:

class Solution {
public:bool isCircularSentence(string sentence) {if  (sentence[0] != sentence[sentence.size() - 1]) {return false;}for (int i = 0; i < sentence.size(); i++) {if (sentence[i] == ' ') {if (sentence[i - 1] != sentence[i + 1]) {return false;}}}return true;}
};

排序后模拟:6254. 划分技能点相等的团队

先排序后,判断第i个和第n-i个加起来的和是否相等,如果不相等,直接返回-1结束。然后把这些加和,返回结果即可。

class Solution {
public:long long dividePlayers(vector& skill) {sort(skill.begin(), skill.end());long long ans = 0;long long temp = skill[0] + skill[skill.size() - 1];for (int i = 0; i < skill.size() / 2; i++) {if (skill[i] + skill[skill.size() - i - 1] != temp) {return -1;} else {ans += skill[i] * skill[skill.size() - i - 1];}}return ans;}
};

BFS:6255. 两个城市间路径的最小分数

class Solution {
public:int minScore(int n, vector>& roads) {vector>> arr(n);for (const auto& r : roads) {int u = r[0] - 1, v = r[1] - 1, d = r[2];arr[u].emplace_back(v, d);arr[v].emplace_back(u, d);}int ret = INT_MAX;vector visit(n);queue q;q.push(0);while (!q.empty()) {int f = q.front();q.pop();if (visit[f] != 0) {continue;}visit[f] = 1;for (auto [next, d] : arr[f]) {q.push(next);ret = min(ret, d);}}return ret;}
};

BFS:6256. 将节点分成尽可能多的组

 贴一个大佬的回答吧:

class Solution {
public:int magnificentSets(int n, vector> &edges) {vector> g(n);for (auto &e : edges) {int x = e[0] - 1, y = e[1] - 1;g[x].push_back(y);g[y].push_back(x);}int time[n], clock = 0; // time 充当 vis 数组的作用(避免在 BFS 内部重复创建 vis 数组)memset(time, 0, sizeof(time));auto bfs = [&](int start) -> int { // 返回从 start 出发的最大深度int depth = 0;time[start] = ++clock;vector q = {start};while (!q.empty()) {vector nxt;for (int x : q)for (int y : g[x])if (time[y] != clock) { // 没有在同一次 BFS 中访问过time[y] = clock;nxt.push_back(y);}q = move(nxt);++depth;}return depth;};int8_t color[n]; memset(color, 0, sizeof(color));vector nodes;function is_bipartite = [&](int x, int8_t c) -> bool { // 二分图判定,原理见视频讲解nodes.push_back(x);color[x] = c;for (int y : g[x])if (color[y] == c || color[y] == 0 && !is_bipartite(y, -c))return false;return true;};int ans = 0;for (int i = 0; i < n; ++i) {if (color[i]) continue;nodes.clear();if (!is_bipartite(i, 1)) return -1; // 如果不是二分图(有奇环),则无法分组// 否则一定可以分组int max_depth = 0;for (int x : nodes) // 枚举连通块的每个点,作为起点 BFS,求最大深度max_depth = max(max_depth, bfs(x));ans += max_depth;}return ans;}
};

https://leetcode.cn/problems/divide-nodes-into-the-maximum-number-of-groups/solution/mei-ju-qi-dian-pao-bfs-by-endlesscheng-s5bu/

 

相关内容

热门资讯

银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...