当前位置:网站首页>LeetCode·每日一题·952.按公因数计算最大组件大小·并查集
LeetCode·每日一题·952.按公因数计算最大组件大小·并查集
2022-07-30 21:05:00 【小迅想变强】
题目
示例
思路
思路:
通过元素间的公因数将彼此连接起来。
1.并查集:
构建时无需按集合大小进行合并,可直接合并。最大的连通组件 可通过对并查集一次遍历求得。
配合路径压缩。见find函数。
2.公因数:
注意循环中 i * i <= num, 如果数据范围更大的话,可能会溢出。 用 i <= num / i 更好。
举例例如:
nums = [4 , 6 , 12, 15]
其中nums中 每个数num 的大于2的因数为:
4 -> 2
6 -> 2, 3
12 -> 2, 3, 4, 6
15 -> 3, 5
建立并查集时,通过 因数 2 可将 4, 6, 12连通。
通过 因数3 可将 6, 12, 15 连通。
而因数 2 和因数 3 也是连通的(如2 是3 的父节点)。
则 原数组中的 四个数连通。其在并查集中 具有相同的父节点。
求解
最后对数组一次遍历,若其父节点相同,则在同一个连通组件中。
更新每个父节点出现的最大次数即可。
代码
#define MAX(a, b) ((a) > (b) ? (a) : (b))
typedef struct UnionFind {
int *parent;
int *rank;
} UnionFind;
UnionFind* unionFindCreate(int n) {
UnionFind *obj = (UnionFind *)malloc(sizeof(UnionFind));
obj->parent = (int *)malloc(sizeof(int) * n);
obj->rank = (int *)malloc(sizeof(int) * n);
memset(obj->rank, 0, sizeof(int) * n);
for (int i = 0; i < n; i++) {
obj->parent[i] = i;
}
return obj;
}
int find(const UnionFind *obj, int x) {
if (obj->parent[x] != x) {
obj->parent[x] = find(obj, obj->parent[x]);
}
return obj->parent[x];
}
void uni(UnionFind *obj, int x, int y) {
int rootx = find(obj, x);
int rooty = find(obj, y);
if (rootx != rooty) {
if (obj->rank[rootx] > obj->rank[rooty]) {
obj->parent[rooty] = rootx;
} else if (obj->rank[rootx] < obj->rank[rooty]) {
obj->parent[rootx] = rooty;
} else {
obj->parent[rooty] = rootx;
obj->rank[rootx]++;
}
}
}
void unionFindFree(UnionFind *obj) {
free(obj->parent);
free(obj->rank);
free(obj);
}
int largestComponentSize(int* nums, int numsSize) {
int m = nums[0];
for (int i = 0; i < numsSize; i++) {
m = MAX(m, nums[i]);
}
UnionFind *uf = unionFindCreate(m + 1);
for (int i = 0; i < numsSize; i++) {
int num = nums[i];
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
uni(uf, num, i);
uni(uf, num, num / i);
}
}
}
int *counts = (int *)malloc(sizeof(int) * (m + 1));
memset(counts, 0, sizeof(int) * (m + 1));
int ans = 0;
for (int i = 0; i < numsSize; i++) {
int root = find(uf, nums[i]);
counts[root]++;
ans = MAX(ans, counts[root]);
}
free(counts);
unionFindFree(uf);
return ans;
}
时间空间复杂度
边栏推荐
猜你喜欢
Why do so many people who teach themselves software testing give up later...
Deep Non-Local Kalman Network for VideoCompression Artifact Reduction
MySQL BIGINT 数据类型
Mysql——字符串函数
数据指标口径不统一、重复开发?亿信ABI指标管理平台帮你解决
Activiti 工作流引擎 详解
【回归预测-lssvm分类】基于最小二乘支持向量机lssvm实现数据分类代码
用于视频压缩伪影消除的深度卡尔曼滤波网络
2021年PHP-Laravel面试题问卷题 答案记录
Babbitt | Metaverse Daily Must Read: The shuffling is coming, will the digital Tibetan industry usher in a new batch of leaders in the second half?Will there be new ways to play?...
随机推荐
用于命名实体识别的模块化交互网络
肖特基二极管厂家ASEMI带你认识电路中的三大重要元器件
KEIL problem: [keil Error: failed to execute 'C:\Keil\ARM\ARMCC']
Image Restoration by Estimating Frequency Distribution of Local Patches
IDEA2018.3.5取消双击Shift快捷键
软考 --- 数据库(6)数据仓库、分布式数据库
mpls简介
Simple configuration of three-tier architecture
[Nuxt 3] (十四) Nuxt 生命周期
Android studio连接MySQL并完成简单的登录注册功能
新书上市 |《谁在掷骰子?》在“不确定性时代”中确定前行
Mysql——字符串函数
R package调试
KingbaseES V8R6备份恢复案例之---同一数据库创建不同stanza备份
@RequestParam使用
使用map函数,对list中的每个元素进行操作 好像不用map
手把手教你搭建一台永久运行的个人服务器
MySQL_关于JSON数据的查询
vlookup函数匹配不出来的原因及解决方法
外包干了三年,废了...