当前位置:网站首页>【952. Calculate the maximum component size according to the common factor】
【952. Calculate the maximum component size according to the common factor】
2022-07-31 00:50:00 【[email protected]】
来源:力扣(LeetCode)
描述:
给定一个由不同正整数的组成的非空数组 nums
,考虑下面的图:
- 有
nums.length
个节点,按从nums[0]
到nums[nums.length - 1]
标记; - 只有当
nums[i]
和nums[j]
共用一个大于 1 的公因数时,nums[i]
和nums[j]
之间才有一条边.
返回 图中最大连通组件的大小 .
示例 1:
输入:nums = [4,6,15,35]
输出:4
示例 2:
输入:nums = [20,50,9,63]
输出:2
示例 3:
输入:nums = [2,3,6,7,4,12,21,39]
输出:8
提示:
- 1 <= nums.length <= 2 * 104
- 1 <= nums[i] <= 105
- nums 中所有值都 不同
方法:并查集
代码:
class UnionFind {
public:
UnionFind(int n) {
parent = vector<int>(n);
rank = vector<int>(n);
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
void uni(int x, int y) {
int rootx = find(x);
int rooty = find(y);
if (rootx != rooty) {
if (rank[rootx] > rank[rooty]) {
parent[rooty] = rootx;
} else if (rank[rootx] < rank[rooty]) {
parent[rootx] = rooty;
} else {
parent[rooty] = rootx;
rank[rootx]++;
}
}
}
int find(int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
private:
vector<int> parent;
vector<int> rank;
};
class Solution {
public:
int largestComponentSize(vector<int>& nums) {
int m = *max_element(nums.begin(), nums.end());
UnionFind uf(m + 1);
for (int num : nums) {
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
uf.uni(num, i);
uf.uni(num, num / i);
}
}
}
vector<int> counts(m + 1);
int ans = 0;
for (int num : nums) {
int root = uf.find(num);
counts[root]++;
ans = max(ans, counts[root]);
}
return ans;
}
};
执行用时:196 ms, 在所有 C++ 提交中击败了71.01%的用户
内存消耗:75.2 MB, 在所有 C++ 提交中击败了27.54%的用户
author:LeetCode-Solution
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/212/202207310039478432.html
边栏推荐
- unity2D横版游戏教程4-物品收集以及物理材质
- 【ABAP】MFBF过账到质量检验库存类型Demo
- 埃拉托斯特尼筛法
- Rocky/GNU之Zabbix部署(1)
- 人工智能与云安全
- Can deep learning solve the parameters of a specific function?
- Summary of MySQL database interview questions (2022 latest version)
- Rocky/GNU之Zabbix部署(3)
- 深度学习可以求解特定函数的参数么?
- WEB Security Basics - - - Vulnerability Scanner
猜你喜欢
registers (assembly language)
mysql索引失效的常见9种原因详解
(5) fastai application
Unity2D horizontal version game tutorial 4 - item collection and physical materials
ShardingSphere之垂直分库分表实战(五)
Rocky/GNU之Zabbix部署(1)
How to Add a Navigation Menu on Your WordPress Site
Mysql systemized JOIN operation example analysis
Error occurred while trying to proxy request项目突然起不来了
MySQL数据库(基础)
随机推荐
Add text watermark to PHP image
Method for deduplication of object collection
Problem record in the use of TypeScript
XSS related knowledge
ShardingSphere之未分片表配置实战(六)
mysql索引失效的常见9种原因详解
24. 请你谈谈单例模式的优缺点,注意事项,使用场景
Solution: Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
registers (assembly language)
Responsive layout vs px/em/rem
C语言力扣第48题之旋转图像。辅助数组
ELK部署脚本---亲测可用
DOM系列之 offset 系列
Kotlin协程:协程上下文与上下文元素
typescript16-void
typescript17 - function optional parameters
Rocky/GNU之Zabbix部署(1)
Redis learning
【多线程】
ShardingSphere之公共表实战(七)