【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 ...
随机推荐
- hdu 2200 Eddy's AC难题(简单数学。。)
题意: N个人,每个人AC的题数都不一样. Eddy想从中选出一部分人(或者全部)分成两组.必须满足第一组中的最小AC数大于第二组中的最大AC数. 问共有多少种不同的选择方案. 思路: 简单数学.. ...
- u-boot 1.1.6 start.S 代码学习<转>
---转自 http://blog.csdn.net/rockhard/article/details/4166642 ------ /* 参考了别人的一些笔记,看完了启动代码. 本文档记录在看代码时 ...
- C# StringBuilder和string
StringBuilder和string 1.string是引用类型还是值类型 MSDN官方说string是引用类型: 引用类型:引用分配栈内存,引用类型本身的数据存储在堆中: 值类型:在函数中创建, ...
- ZooKeeper 集群搭建 Error contacting service. It is probably not running.
搭建环境:Centos 7 虚拟机 3台 按照此教程搭建:https://www.ilanni.com/?p=11393 之后出现错误:Error contacting service. It is ...
- 攻防世界 WEB 高手进阶区 XCTF 4th-CyberEarth ics-06 Writeup
攻防世界 WEB 高手进阶区 XCTF 4th-CyberEarth ics-06 Writeup 题目介绍 题目考点 掌握暴力破解手段 Writeup 打开链接 http://220.249.52. ...
- Java反射判断对象实例所有属性是否为空
https://www.jb51.net/article/201647.htm public static Boolean ObjectAllFieldsEmpty(Object obj) throw ...
- Jackson & fastJson的使用
Jackson import lombok.Data; @Data public class Student { private Long id; private String name; priva ...
- tomcat9启动报错too low setting for -Xss
在tomcat下部署war包启动时报错,关键错误信息如下: Caused by: java.lang.IllegalStateException: Unable to complete the sca ...
- 一看就懂的IdentityServer4认证授权设计方案
查阅了大多数相关资料,总结设计一个IdentityServer4认证授权方案,我们先看理论,后设计方案. 1.快速理解认证授权 我们先看一下网站发起QQ认证授权,授权通过后获取用户头像,昵称的流程. ...
- 基于hadoop_yarn的资源隔离配置
目录 yarn的基本概念 scheduler 集群整体的资源定义 fair scheduler简介 配置demo 队列的资源限制 基于具体资源限制 基于权重资源限制 队列运行状态限制 基于用户和分组限 ...