当前位置:网站首页>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

边栏推荐
- 软考 --- 数据库(6)数据仓库、分布式数据库
- (7/29) Basic board minimum spanning tree prim+kruskal
- 对一次生产环境产生OOM的记录,结论:除非在自己完全有把握的情况下,否则不要偷懒查询无用字段
- kubernetes
- [Machine Learning] The Beauty of Mathematics Behind Gradient Descent
- Apache DolphinScheduler新一代分布式工作流任务调度平台实战-上
- 用于命名实体识别的模块化交互网络
- 2.网络资源访问工具:requests
- Deep Kalman Filter Network for Video Compression Artifact Removal
- 冲刺第六周
猜你喜欢
随机推荐
Google Earth Engine ——ee.List.sequence函数的使用
基于ABP实现DDD--领域逻辑和应用逻辑
关于SFML Rect.inl文件报错的问题
这本记述40年前历史的游戏书,预言的却是当下的事
[The Beauty of Software Engineering - Column Notes] 31 | Is software testing responsible for product quality?
Activiti 工作流引擎 详解
go慢速入门——函数
《快速掌握QML》第六章 动画
DPW-SDNet: Dual Pixel-Wavelet Domain Deep CNNs for Soft Decoding of JPEG-Compressed Images
Deep Non-Local Kalman Network for VideoCompression Artifact Reduction
[Nuxt 3] (十三) Nuxt 是如何工作的?
MySQL60 homework
chrome extension: how to make the dialog be on the right side of the current window?
Oracle ADG状态查看与相关视图
冲刺第六周
微信公众号授权登录后报redirect_uri参数错误的问题
Motion Tuned Spatio-temporal Quality Assessmentof Natural Videos
MySQL笔记1(数据库的好处,数据库的概念,数据库的特点,MySQL的启动,数据模型,SQL)
How do I refresh the company's background management system (Part 1) - performance optimization
【Nacos】解决Nacos下载速度缓慢的问题








