【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 ...
随机推荐
- PHP笔记4__函数/全局、静态变量/函数参数/加载函数库/,,
<?php header("Content-type: text/html; charset=utf-8"); echo table(5,5); function table ...
- DeWeb : 制作图片轮换效果
演示:http://www.web0000.com/slide.dw源代码:http://www.web0000.com/media/source/slide.zip一.新建一个DLL二.除第一行外, ...
- Redis监控调研
1 调研目的 主要的目的是想调研各大云平台有关Redis监控功能的实现,但是最后我发现各大云平台提供的监控功能都比较基础,比如我想看诸如访问频率较高的HotKey.占用内存较大的Bigkey等指标,它 ...
- 攻防世界 Misc 新手练习区 ext3 bugku Writeup
攻防世界 Misc 新手练习区 ext3 bugku Writeup 题目介绍 题目考点 WinHex工具的使用 linux磁盘挂载mount命令 Writeup 下载附件拖进winhex分析一下,查 ...
- PAT甲级1074 Reversing Linked List (25分)
[程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点 注意:输入的节点中有可能存在无用节点 ...
- 『与善仁』Appium基础 — 9、补充:C/S架构和B/S架构说明
目录 1.C/S架构和B/S架构概念 2.C/S结构与B/S架构的区别 3.C/S架构和B/S架构优点和缺点 (1)B/S模式的优点和缺点: (2)C/S模式的优点和缺点: 1.C/S架构和B/S架构 ...
- 浅谈web前端优化
开篇 优化网站是一个系统性和持续性的过程.很多人认为优化网站的性能只需要合并图片啦,减小HTTP请求啦,部署CDN啦就行,实际上这都是见木不见林的做法.以上的做法经常会被面试者提起,在被问到自己在网页 ...
- Python 爬取 煎蛋
这个我是拿来参考的 import requests def url_open(url): response = requests.get(url) html = response.content re ...
- 填坑总结:python内存泄漏排查小技巧
摘要:最近服务遇到了内存泄漏问题,运维同学紧急呼叫解决,于是在解决问题之余也系统记录了下内存泄漏问题的常见解决思路. 本文分享自华为云社区<python内存泄漏排查小技巧>,作者:luti ...
- [Git专题] 环境搭建
环境搭建 在正式使用 Git 之前,首先应当安装 Git 并完成一些基础配置,本章内容就教大家在 Ubuntu 和 CentOS 上安装 Git 的方法. 安装 Git 客户端 如果你使用的是基于 D ...