From 8cbf1427f9ff7b79b793ba96ecb0fdee9ff6cfca Mon Sep 17 00:00:00 2001 From: YDZ Date: Thu, 22 Aug 2019 18:12:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20problem=20952?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...Largest Component Size by Common Factor.go | 51 +++++++++++++++++ ...st Component Size by Common Factor_test.go | 56 +++++++++++++++++++ .../README.md | 56 +++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 Algorithms/0952. Largest Component Size by Common Factor/952. Largest Component Size by Common Factor.go create mode 100644 Algorithms/0952. Largest Component Size by Common Factor/952. Largest Component Size by Common Factor_test.go create mode 100755 Algorithms/0952. Largest Component Size by Common Factor/README.md diff --git a/Algorithms/0952. Largest Component Size by Common Factor/952. Largest Component Size by Common Factor.go b/Algorithms/0952. Largest Component Size by Common Factor/952. Largest Component Size by Common Factor.go new file mode 100644 index 00000000..2880009c --- /dev/null +++ b/Algorithms/0952. Largest Component Size by Common Factor/952. Largest Component Size by Common Factor.go @@ -0,0 +1,51 @@ +package leetcode + +// 解法一 并查集 UnionFind +func largestComponentSize(A []int) int { + maxElement, uf, countMap, res := 0, UnionFind{}, map[int]int{}, 1 + for _, v := range A { + maxElement = max(maxElement, v) + } + uf.init(maxElement + 1) + for _, v := range A { + for k := 2; k*k <= v; k++ { + if v%k == 0 { + uf.union(v, k) + uf.union(v, v/k) + } + } + } + for _, v := range A { + countMap[uf.find(v)]++ + res = max(res, countMap[uf.find(v)]) + } + return res +} + +// 解法二 UnionFindCount +func largestComponentSize1(A []int) int { + uf, factorMap := UnionFindCount{}, map[int]int{} + uf.init(len(A)) + for i, v := range A { + for k := 2; k*k <= v; k++ { + if v%k == 0 { + if _, ok := factorMap[k]; !ok { + factorMap[k] = i + } else { + uf.union(i, factorMap[k]) + } + if _, ok := factorMap[v/k]; !ok { + factorMap[v/k] = i + } else { + uf.union(i, factorMap[v/k]) + } + } + } + if _, ok := factorMap[v]; !ok { + factorMap[v] = i + } else { + uf.union(i, factorMap[v]) + } + } + return uf.maxUnionCount +} diff --git a/Algorithms/0952. Largest Component Size by Common Factor/952. Largest Component Size by Common Factor_test.go b/Algorithms/0952. Largest Component Size by Common Factor/952. Largest Component Size by Common Factor_test.go new file mode 100644 index 00000000..7c05beb7 --- /dev/null +++ b/Algorithms/0952. Largest Component Size by Common Factor/952. Largest Component Size by Common Factor_test.go @@ -0,0 +1,56 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question952 struct { + para952 + ans952 +} + +// para 是参数 +// one 代表第一个参数 +type para952 struct { + one []int +} + +// ans 是答案 +// one 代表第一个答案 +type ans952 struct { + one int +} + +func Test_Problem952(t *testing.T) { + + qs := []question952{ + question952{ + para952{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9}}, + ans952{6}, + }, + + question952{ + para952{[]int{4, 6, 15, 35}}, + ans952{4}, + }, + + question952{ + para952{[]int{20, 50, 9, 63}}, + ans952{2}, + }, + + question952{ + para952{[]int{2, 3, 6, 7, 4, 12, 21, 39}}, + ans952{8}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 952------------------------\n") + + for _, q := range qs { + _, p := q.ans952, q.para952 + fmt.Printf("【input】:%v 【output】:%v\n", p, largestComponentSize(p.one)) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/0952. Largest Component Size by Common Factor/README.md b/Algorithms/0952. Largest Component Size by Common Factor/README.md new file mode 100755 index 00000000..52074d79 --- /dev/null +++ b/Algorithms/0952. Largest Component Size by Common Factor/README.md @@ -0,0 +1,56 @@ +# [952. Largest Component Size by Common Factor](https://leetcode.com/problems/largest-component-size-by-common-factor/) + + +## 题目: + +Given a non-empty array of unique positive integers `A`, consider the following graph: + +- There are `A.length` nodes, labelled `A[0]` to `A[A.length - 1];` +- There is an edge between `A[i]` and `A[j]` if and only if `A[i]`and `A[j]` share a common factor greater than 1. + +Return the size of the largest connected component in the graph. + +**Example 1:** + + Input: [4,6,15,35] + Output: 4 + +![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/01/ex1.png) + +**Example 2:** + + Input: [20,50,9,63] + Output: 2 + +![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/01/ex2.png) + +**Example 3:** + + Input: [2,3,6,7,4,12,21,39] + Output: 8 + +![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/01/ex3.png) + +**Note:** + +1. `1 <= A.length <= 20000` +2. `1 <= A[i] <= 100000` + + +## 题目大意 + +给定一个由不同正整数的组成的非空数组 A,考虑下面的图: + +有 A.length 个节点,按从 A[0] 到 A[A.length - 1] 标记; +只有当 A[i] 和 A[j] 共用一个大于 1 的公因数时,A[i] 和 A[j] 之间才有一条边。 +返回图中最大连通组件的大小。 + +提示: + +1. 1 <= A.length <= 20000 +2. 1 <= A[i] <= 100000 + +## 解题思路 + +- 给出一个数组,数组中的元素如果每两个元素有公约数,那么这两个元素可以算有关系。所有有关系的数可以放在一个集合里,问这个数组里面有关系的元素组成的集合里面最多有多少个元素。 +- 这一题读完题直觉就是用并查集来解题。首先可以用暴力的解法尝试。用 2 层循环,两两比较有没有公约数,如果有公约数就 `union()` 到一起。提交以后出现 TLE,其实看一下数据规模就知道会超时,`1 <= A.length <= 20000`。注意到 `1 <= A[i] <= 100000`,开根号以后最后才 316.66666,这个规模的数不大。所以把每个数小于根号自己的因子都找出来,例如 `6 = 2 * 3`,`15 = 3 * 5`,那么把 6 和 2,6 和 3 都 `union()`,15 和 3,15 和 5 都 `union()`,最终遍历所有的集合,找到最多元素的集合,输出它包含的元素值。