当前位置:网站首页>LeetCode·Daily Question·952. Calculate Maximum Component Size by Common Factor·Union Check
LeetCode·Daily Question·952. Calculate Maximum Component Size by Common Factor·Union Check
2022-07-30 21:17:00 【Xiao Xun wants to become stronger】
Title

Example

Thoughts
Idea:
Elements are connected to each other by a common factor between them.
1. And search:
There is no need to merge by collection size when building, it can be merged directly.The largest connected component can be found by traversing the union search set once.
Works with path compression.See find function.
2. Common factor:
Note that i * i <= num in the loop, if the data range is larger, it may overflow.Better to use i <= num / i .
For example:
nums = [4 , 6 , 12, 15]
where the factor of each number num in nums greater than 2 is:
4 -> 2
6 -> 2, 3
12 -> 2, 3, 4, 6
15 -> 3, 5
When creating and checking the set, the factor 2 can be used to convert 4, 6, 12 are connected.
6, 12, 15 can be connected by a factor of 3.
And factor 2 and factor 3 are also connected (eg 2 is the parent of 3).
Then the four numbers in the original array are connected.It has the same parent node in the union.
Solve
Finally, the array is traversed once, if its parent node is the same, it is in the same connected component.
Just update the maximum number of occurrences of each parent node.
Code
#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;}Time and space complexity

边栏推荐
- Image Restoration by Estimating Frequency Distribution of Local Patches
- [Deep Learning] Target Detection | SSD Principle and Implementation
- c语言:操作符详解
- JSESSIONID description in cookie
- R package调试
- awk notes
- y82.第四章 Prometheus大厂监控体系及实战 -- 监控扩展和prometheus 联邦(十三)
- 我是如何让公司后台管理系统焕然一新的(上) -性能优化
- JDBC (detailed explanation)
- 外包干了三年,废了...
猜你喜欢
随机推荐
Quick Master QML Chapter 6 Animation
GPGGA NTRIP RTCM Notes
MySQL60题作业
js堆和栈
Google Earth Engine ——我们如何筛选一个列表中的排序以时间为例
2021年软件测试面试题大全
《快速掌握QML》第六章 动画
微信读书,导出笔记
C语言中指针没那么难~(2)【文章结尾有资料】
socket:内核初始化及创建流(文件)详细过程
Google Earth Engine ——快速实现MODIS影像NDVI动画的在线加载并导出
y82.第四章 Prometheus大厂监控体系及实战 -- 监控扩展和prometheus 联邦(十三)
MySQL60 homework
JSESSIONID description in cookie
微信公众号授权登录后报redirect_uri参数错误的问题
关于SFML Rect.inl文件报错的问题
我是如何让公司后台管理系统焕然一新的(上) -性能优化
Redis数据更新,是先更新数据库还是先更新缓存?
The structure of knowledge in the corners of the C language
2022-07-29 mysql/stonedb慢SQL-Q17-分析









