当前位置:网站首页>【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
边栏推荐
- MySQL triggers
- Common network status codes
- MySQL master-slave replication and read-write separation script - pro test available
- MySQL database (basic)
- binglog log tracking: data backup and backup tracking
- Consistency and Consensus of Distributed Systems (1) - Overview
- How to use joiplay emulator
- Basic usage of async functions and await expressions in ES6
- IOT跨平台组件设计方案
- software development design process
猜你喜欢

Error occurred while trying to proxy request项目突然起不来了

go mode tidy出现报错go warning “all“ matched no packages

How to solve types joiplay simulator does not support this game

encrypted transmission process

Thesis understanding: "Designing and training of a dual CNN for image denoising"

Summary of MySQL database interview questions (2022 latest version)

Niuke.com question brushing training (4)

what is jira

小程序-全局数据共享

DATA AI Summit 2022提及到的对 aggregate 的优化
随机推荐
SereTOD2022 Track2代码剖析-面向半监督和强化学习的任务型对话系统挑战赛
正则表达式密码策略与正则回溯机制绕过
Thesis understanding: "Designing and training of a dual CNN for image denoising"
Go 学习笔记(84)— Go 项目目录结构
[In-depth and easy-to-follow FPGA learning 13---------Test case design 1]
Jetpack Compose学习(8)——State及remeber
Optimization of aggregate mentioned at DATA AI Summit 2022
【Demo】ABAP Base64加解密测试
【多线程】
XSS related knowledge
MySQL系列一:账号管理与引擎
redis学习
What are the efficient open source artifacts of VSCode
Restricted character bypass
【Yugong Series】July 2022 Go Teaching Course 019-For Circular Structure
web漏洞之需要准备的工作
【Yugong Series】July 2022 Go Teaching Course 017-IF of Branch Structure
【深入浅出玩转FPGA学习13-----------测试用例设计1】
Steven Giesel recently published a 5-part series documenting his first experience building an application with the Uno Platform.
【深度学习】Transformer模型详解
