Skip to content

Commit 8cbf142

Browse files
committed
添加 problem 952
1 parent 0430798 commit 8cbf142

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package leetcode
2+
3+
// 解法一 并查集 UnionFind
4+
func largestComponentSize(A []int) int {
5+
maxElement, uf, countMap, res := 0, UnionFind{}, map[int]int{}, 1
6+
for _, v := range A {
7+
maxElement = max(maxElement, v)
8+
}
9+
uf.init(maxElement + 1)
10+
for _, v := range A {
11+
for k := 2; k*k <= v; k++ {
12+
if v%k == 0 {
13+
uf.union(v, k)
14+
uf.union(v, v/k)
15+
}
16+
}
17+
}
18+
for _, v := range A {
19+
countMap[uf.find(v)]++
20+
res = max(res, countMap[uf.find(v)])
21+
}
22+
return res
23+
}
24+
25+
// 解法二 UnionFindCount
26+
func largestComponentSize1(A []int) int {
27+
uf, factorMap := UnionFindCount{}, map[int]int{}
28+
uf.init(len(A))
29+
for i, v := range A {
30+
for k := 2; k*k <= v; k++ {
31+
if v%k == 0 {
32+
if _, ok := factorMap[k]; !ok {
33+
factorMap[k] = i
34+
} else {
35+
uf.union(i, factorMap[k])
36+
}
37+
if _, ok := factorMap[v/k]; !ok {
38+
factorMap[v/k] = i
39+
} else {
40+
uf.union(i, factorMap[v/k])
41+
}
42+
}
43+
}
44+
if _, ok := factorMap[v]; !ok {
45+
factorMap[v] = i
46+
} else {
47+
uf.union(i, factorMap[v])
48+
}
49+
}
50+
return uf.maxUnionCount
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question952 struct {
9+
para952
10+
ans952
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para952 struct {
16+
one []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans952 struct {
22+
one int
23+
}
24+
25+
func Test_Problem952(t *testing.T) {
26+
27+
qs := []question952{
28+
question952{
29+
para952{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9}},
30+
ans952{6},
31+
},
32+
33+
question952{
34+
para952{[]int{4, 6, 15, 35}},
35+
ans952{4},
36+
},
37+
38+
question952{
39+
para952{[]int{20, 50, 9, 63}},
40+
ans952{2},
41+
},
42+
43+
question952{
44+
para952{[]int{2, 3, 6, 7, 4, 12, 21, 39}},
45+
ans952{8},
46+
},
47+
}
48+
49+
fmt.Printf("------------------------Leetcode Problem 952------------------------\n")
50+
51+
for _, q := range qs {
52+
_, p := q.ans952, q.para952
53+
fmt.Printf("【input】:%v 【output】:%v\n", p, largestComponentSize(p.one))
54+
}
55+
fmt.Printf("\n\n\n")
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# [952. Largest Component Size by Common Factor](https://leetcode.com/problems/largest-component-size-by-common-factor/)
2+
3+
4+
## 题目:
5+
6+
Given a non-empty array of unique positive integers `A`, consider the following graph:
7+
8+
- There are `A.length` nodes, labelled `A[0]` to `A[A.length - 1];`
9+
- 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.
10+
11+
Return the size of the largest connected component in the graph.
12+
13+
**Example 1:**
14+
15+
Input: [4,6,15,35]
16+
Output: 4
17+
18+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/01/ex1.png)
19+
20+
**Example 2:**
21+
22+
Input: [20,50,9,63]
23+
Output: 2
24+
25+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/01/ex2.png)
26+
27+
**Example 3:**
28+
29+
Input: [2,3,6,7,4,12,21,39]
30+
Output: 8
31+
32+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/01/ex3.png)
33+
34+
**Note:**
35+
36+
1. `1 <= A.length <= 20000`
37+
2. `1 <= A[i] <= 100000`
38+
39+
40+
## 题目大意
41+
42+
给定一个由不同正整数的组成的非空数组 A,考虑下面的图:
43+
44+
有 A.length 个节点,按从 A[0] 到 A[A.length - 1] 标记;
45+
只有当 A[i] 和 A[j] 共用一个大于 1 的公因数时,A[i] 和 A[j] 之间才有一条边。
46+
返回图中最大连通组件的大小。
47+
48+
提示:
49+
50+
1. 1 <= A.length <= 20000
51+
2. 1 <= A[i] <= 100000
52+
53+
## 解题思路
54+
55+
- 给出一个数组,数组中的元素如果每两个元素有公约数,那么这两个元素可以算有关系。所有有关系的数可以放在一个集合里,问这个数组里面有关系的元素组成的集合里面最多有多少个元素。
56+
- 这一题读完题直觉就是用并查集来解题。首先可以用暴力的解法尝试。用 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()`,最终遍历所有的集合,找到最多元素的集合,输出它包含的元素值。

0 commit comments

Comments
 (0)