当前位置:网站首页>【952. 按公因数计算最大组件大小】
【952. 按公因数计算最大组件大小】
2022-07-31 00:40:00 【千北@】
来源:力扣(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
边栏推荐
- SWM32 Series Tutorial 6 - Systick and PWM
- Kotlin协程:协程上下文与上下文元素
- 解决:Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
- Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv
- How to import game archives in joiplay emulator
- Adding, deleting, modifying and checking the foundation of MySQL
- MySQL数据库面试题总结(2022最新版)
- 分布式系统的一致性与共识(1)-综述
- asser利用蚁剑登录
- Strict Mode for Databases
猜你喜欢

小程序-全局数据共享

Encapsulate and obtain system user information, roles and permission control

Detailed explanation of 9 common reasons for MySQL index failure

encrypted transmission process

消息队列存储消息数据的MySQL表设计

论文理解:“Designing and training of a dual CNN for image denoising“

DNS解析过程【访问网站】
![[C language course design] C language campus card management system](/img/36/9e1a97767bbe1e504d8a3cc3ce5448.png)
[C language course design] C language campus card management system

WMware Tools installation failed segmentation fault solution

会议OA项目待开会议、所有会议功能
随机推荐
【深度学习】Transformer模型详解
Filter (Filter)
MySQL数据库的truncate与delete区别
MySql数据恢复方法个人总结
Summary of MySQL database interview questions (2022 latest version)
Meeting OA project pending meeting, all meeting functions
registers (assembly language)
[In-depth and easy-to-follow FPGA learning 14----------Test case design 2]
go mode tidy出现报错go warning “all“ matched no packages
XSS related knowledge
Asser uses ant sword to log in
MySQL database (basic)
h264和h265解码上的区别
Niuke.com question brushing training (4)
[Deep learning] Detailed explanation of Transformer model
Bypass of xss
【愚公系列】2022年07月 Go教学课程 017-分支结构之IF
The difference between substring and substr in MySQL
[C language course design] C language campus card management system
Strict Mode for Databases
