【leetcode】952. Largest Component Size by Common Factor(Union find)
You are given an integer array of unique positive integers nums. Consider the following graph:
- There are
nums.lengthnodes, labelednums[0]tonums[nums.length - 1], - There is an undirected edge between
nums[i]andnums[j]ifnums[i]andnums[j]share a common factor greater than1.
Return the size of the largest connected component in the graph.
Example 2:

Input: nums = [20,50,9,63]
Output: 2
这道题的含义是,对于一串数字,如果两两之间存在相同的大于1的的公因子,则这两个数可以当作为一组,同时如果A与B有大于1的公因子,B与C有大于1的公因子,则A、B、C可以当作一组,此时长度就是3,
这道题虽然是求Graph的长度,但按照上述的分析来看,其实就是将数组元素分组,然后求同一类元素的个数,对于这种题目优先考虑使用并查集。
并查集讲解帖子,对于一个并查集,主要做两步,Union 以及find。一般最简单的并查集包含以下几个步骤:
1) 初始化
假如有编号为1, 2, 3, ..., n的n个元素,我们用一个数组fa[]来存储每个元素的父节点(因为每个元素有且只有一个父节点,所以这是可行的)。一开始,我们先将它们的父节点设为自己。
int fa[MAXN];
void init(int n)
{
for (int i = 1; i <= n; ++i)
fa[i] = i;
}
2)查询
用递归的写法实现对代表元素的查询:一层一层访问父节点,直至根节点(根节点的标志就是父节点是本身)。要判断两个元素是否属于同一个集合,只需要看它们的根节点是否相同即可。
int find(int x)
{
if(fa[x] == x)
return x;
else
return find(fa[x]);
}
对于这个一般可以路径压缩
int find(int x)
{
if(x == fa[x])
return x;
else{
fa[x] = find(fa[x]); //父节点设为根节点
return fa[x]; //返回父节点
}
}
以上代码常常简写为一行:
int find(int x)
{
return x == fa[x] ? x : (fa[x] = find(fa[x]));
}
3)合并
两个不同的元素置为相同的父节点。合并操作也是很简单的,先找到两个集合的代表元素,然后将前者的父节点设为后者即可。
inline void merge(int i, int j)
{
fa[find(i)] = find(j);
}
4) 本题思路
按照上述并查集的构建思路,对于所有元素,初始化一个数组用于存储当前元素和哪些元素通过公因子构成一组,然后统计每一组的元素个数,返回最大的个数值。
class Solution {
public:
int largestComponentSize(vector<int>& A) {
int n = 0, mx = 0, res = 0;
unordered_map<int, int> m;
for (int num : A) mx = max(mx, num); //数组长度
vector<int> root(mx + 1); //初始化union 数组
for (int i = 1; i <= mx; ++i) root[i] = i; //开始每个元素的头节点都指向自己
for (int num : A) {
for (int d = sqrt(num); d >= 2; --d) { //寻找每个元素的公因子
if (num % d == 0) {
root[find(root, num)] = root[find(root, d)]; //将公因子的父节点合并
root[find(root, num)] = root[find(root, num / d)];
}
}
}
for (int num : A) {
res = max(res, ++m[find(root, num)]); //统计不同元素的相同父节点个数 作为最大长度
}
return res;
}
int find(vector<int>& root, int x) {
return root[x] == x ? x : (root[x] = find(root, root[x])); //在查询每个节点的父节点时必须压缩路径
}
};
【leetcode】952. Largest Component Size by Common Factor(Union find)的更多相关文章
- 【LeetCode】952. Largest Component Size by Common Factor 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetco ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- [Swift]LeetCode952. 按公因数计算最大组件大小 | Largest Component Size by Common Factor
Given a non-empty array of unique positive integers A, consider the following graph: There are A.len ...
- 【LeetCode】382. Linked List Random Node 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组保存再随机选择 蓄水池抽样 日期 题目地址:ht ...
- 【LeetCode】232. Implement Queue using Stacks 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Python解法 Java解法 日期 [LeetCo ...
- 【LeetCode】993. Cousins in Binary Tree 解题报告(C++ & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【LeetCode】983. 最低票价 Minimum Cost For Tickets(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- 【LeetCode】695. Max Area of Island 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- 【LeetCode】496. Next Greater Element I 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接遍历查找 字典保存位置 日期 题目地址:http ...
随机推荐
- axios & fetch 异步请求
// 一.创建实例 const request = axios.create({ baseURL: "http://kg.zhaodashen.cn/v2", headers: { ...
- docker 使用报错的相关问题
docker 创建本地主机实例Virtualbox 驱动报错,显示没有下载这个驱动 解决方案,下载virtuabox. https://www.cnblogs.com/effortday/p/1502 ...
- 一条指令优化引发的血案,性能狂掉50%,clang使用-ffast-math选项后变傻了
https://www.cnblogs.com/bbqzsl/p/15510377.html 近期在做优化时,对一些函数分别在不同编译平台上进行bench测试.发现了不少问题. 现在拿其中一个问题来分 ...
- SpringBoot 中发布ApplicationEventPublisher,监听ApplicationEvent 异步操作
有这么一个业务场景:当用户注册后,发送邮件到其邮箱提示用户进行账号激活,且注册成功的同时需要赠送新人用户体验卡券. 业务有了,那么问题也就来了. What? 问题....问题?我听说你有问题? 来拔刀 ...
- Java开发介绍之JDK JRE JVM 和 环境变量配置
一.JDK>JRE>JVM JDK(Java Development Kit):Java开发工具包 JDK中包含JRE,在JDK的安装目录下有一个名为jre的目录,里面有两个文件夹bin和 ...
- Salesforce Consumer Goods Cloud 浅谈篇一之基础介绍
本篇参考: https://baike.baidu.com/item/%E6%B6%88%E8%B4%B9%E5%93%81/425802?fr=aladdin https://help.salesf ...
- Python基础(定制类)
文章转载自廖雪峰老师Python课程博客,仅供学习参考使用看到类似__slots__这种形如__xxx__的变量或者函数名就要注意,这些在Python中是有特殊用途的. __slots__我们已经知道 ...
- Roslyn 编译器Api妙用:动态生成类并实现接口
在上一篇文章中有讲到使用反射手写IL代码动态生成类并实现接口. 反射的妙用:C#通过反射动态生成类型继承接口并实现 有位网友推荐使用 Roslyn 去脚本化动态生成,今天这篇文章就主要讲怎么使用 Ro ...
- 【拥抱元宇宙】创建你的第一个Unity程序HelloWorld,并发布
第一个Unity程序--Hello World. 1.需要先下载一个Unity Hub,以及安装Unity编辑器.Unity Hub需要登陆,激活码可以选择个人用户,免费的.免费的无法改变启动画面,其 ...
- C# 从 UTF-8 流中读取字符串的正确方法
我们下面的代码是从一个流 stream 中读取 UTF-8 编码的字符串.我们可以先考虑一下其中存在的潜在问题. string ReadString(Stream stream) { var sb = ...