当前位置:网站首页>【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
边栏推荐
- Preparations for web vulnerabilities
- Zabbix干啥用?
- 查看zabbix-release-5.0-1.el8.noarch.rpm包内容
- DOM系列之scroll系列
- 【ABAP】MFBF过账到质量检验库存类型Demo
- A complete guide to avoiding pitfalls for the time-date type "yyyy-MM-dd HHmmss" in ES
- ES6中 async 函数、await表达式 的基本用法
- typescript15- (specify both parameter and return value types)
- BOM系列之Navigator对象
- Bypass of xss
猜你喜欢

Shell编程之条件语句

Shell programming of conditional statements
![[In-depth and easy-to-follow FPGA learning 13---------Test case design 1]](/img/1c/a88ba3b01d2e2302c26ed5f730b956.png)
[In-depth and easy-to-follow FPGA learning 13---------Test case design 1]

DOM系列之动画函数封装

typescript12 - union types

What is Promise?What is the principle of Promise?How to use Promises?

Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv

网站频繁出现mysql等数据库连接失败等信息解决办法

DOM系列之 offset 系列

华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘
随机推荐
Consistency and Consensus of Distributed Systems (1) - Overview
Basic usage of async functions and await expressions in ES6
MySQL Series 1: Account Management and Engine
SereTOD2022 Track2 Code Analysis - Task-based Dialogue Systems Challenge for Semi-Supervised and Reinforcement Learning
MySQL——数据库的查,增,删
MySQL database advanced articles
(5) fastai application
程序员工作三年攒多少钱合适?
GO GOPROXY代理设置
响应式布局与px/em/rem的比对
图像处理工具设计
【愚公系列】2022年07月 Go教学课程 013-常量、指针
ShardingSphere read-write separation (8)
WMware Tools安装失败segmentation fault解决方法
这个项目太有极客范儿了
C语言力扣第48题之旋转图像。辅助数组
[Yugong Series] July 2022 Go Teaching Course 015-Assignment Operators and Relational Operators of Operators
The difference between truncate and delete in MySQL database
TypeScript在使用中出现的问题记录
The difference between h264 and h265 decoding
