diff --git a/August-LeetCoding-Challenge/30-Largest-Component-Size-by-Common-Factor/Largest-Component-Size-by-Common-Factor.cs b/August-LeetCoding-Challenge/30-Largest-Component-Size-by-Common-Factor/Largest-Component-Size-by-Common-Factor.cs new file mode 100644 index 0000000..9538611 --- /dev/null +++ b/August-LeetCoding-Challenge/30-Largest-Component-Size-by-Common-Factor/Largest-Component-Size-by-Common-Factor.cs @@ -0,0 +1,51 @@ +public class Solution { + int[] par; + int[] cnt; + public int LargestComponentSize(int[] A) { + var n = A.Length; + par = new int[n]; + cnt = new int[n]; + var dict = new Dictionary>(); + for(int i=0; i()); + dict[d].Add(i); + } + d++; + } + if(x>1){ + if(!dict.ContainsKey(x)) + dict.Add(x, new HashSet()); + dict[x].Add(i); + } + } + + for(int i=0; i h in dict.Values){ + int fir = h.First(); + foreach(var idx in h){ + Union(idx, fir); + max = Math.Max(cnt[Find(idx)], max); + } + } + return max; + } + private void Union(int i, int j){ + int pi = Find(i); + int pj = Find(j); + if(pi == pj) return ; + par[pi] = pj; + cnt[pj] += cnt[pi]; + } + + private int Find(int i){ + if(i==par[i]) return i; + return par[i] = Find(par[i]); + } +} \ No newline at end of file diff --git a/README.md b/README.md index f7fa720..a349120 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ Solutions in various programming languages are provided. Enjoy it. 27. [Find Right Interval](https://github.com/AlgoStudyGroup/Leetcode/tree/master/August-LeetCoding-Challenge/27-Find-Right-Interval) 28. [Implement Rand10() Using Rand7()](https://github.com/AlgoStudyGroup/Leetcode/tree/master/August-LeetCoding-Challenge/28-Implement-Rand10()-Using-Rand7()) 29. [Pancake Sorting](https://github.com/AlgoStudyGroup/Leetcode/tree/master/August-LeetCoding-Challenge/29-Pancake-Sorting) +30. [Largest Component Size by Common Factor](https://github.com/AlgoStudyGroup/Leetcode/tree/master/August-LeetCoding-Challenge/30-Largest-Component-Size-by-Common-Factor) ## July LeetCoding Challenge Click [here](https://leetcode.com/explore/featured/card/july-leetcoding-challenge/) for problem descriptions.