Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit a1f498c

Browse files
committed
952 Time Limit Exceeded
1 parent a1b6db3 commit a1f498c

File tree

3 files changed

+141
-1
lines changed

3 files changed

+141
-1
lines changed
Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,87 @@
11
package problem0952
22

33
func largestComponentSize(A []int) int {
4+
size := len(A)
5+
u := newUnion(size)
46

5-
return 0
7+
for i := 0; i < size-1; i++ {
8+
for j := i + 1; j < size; j++ {
9+
a, b := A[i], A[j]
10+
if gcd(a, b) > 1 {
11+
u.union(i, j)
12+
}
13+
}
14+
}
15+
16+
return u.max
17+
}
18+
19+
// Greatest Common Divisor(GCD)
20+
func gcd(a, b int) int {
21+
if a < b {
22+
return gcd(b, a)
23+
}
24+
for b != 0 {
25+
a, b = b, a%b
26+
}
27+
return a
28+
}
29+
30+
// Robert Sedgewick 算法(第4版) 1.5.2.7
31+
// union-find (加权 quick-union),还作了路径压缩优化
32+
33+
type union struct {
34+
id []int // 父链接数组(由触点索引)
35+
sz []int // (由触点索引的) 各个根节点所对应的分量的大小
36+
max int // 连通分量的数量
37+
}
38+
39+
func newUnion(N int) *union {
40+
id := make([]int, N)
41+
for i := range id {
42+
id[i] = i
43+
}
44+
sz := make([]int, N)
45+
for i := range sz {
46+
sz[i] = 1
47+
}
48+
return &union{
49+
id: id,
50+
sz: sz,
51+
max: 1,
52+
}
53+
}
54+
55+
func (u *union) isConnected(p, q int) bool {
56+
return u.find(p) == u.find(q)
57+
}
58+
59+
func (u *union) find(p int) int {
60+
// 跟随连接找到根节点
61+
for p != u.id[p] {
62+
p = u.id[p]
63+
}
64+
return p
65+
}
66+
67+
func (u *union) union(p, q int) {
68+
i, j := u.find(p), u.find(q)
69+
if i == j {
70+
return
71+
}
72+
if u.sz[i] > u.sz[j] {
73+
i, j = j, i
74+
}
75+
// 将小树的根节点连接到大树的根节点
76+
u.id[i] = j
77+
u.sz[j] += u.sz[i]
78+
u.max = max(u.max, u.sz[j])
79+
return
80+
}
81+
82+
func max(a, b int) int {
83+
if a > b {
84+
return a
85+
}
86+
return b
687
}

Algorithms/0952.largest-component-size-by-common-factor/largest-component-size-by-common-factor_test.go

Lines changed: 5 additions & 0 deletions
Large diffs are not rendered by default.

routine/union-find/union-find.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package unionfind
2+
3+
// Robert Sedgewick 算法(第4版) 1.5.2.7
4+
// union-find (加权 quick-union),还作了路径压缩优化
5+
6+
// union is ...
7+
type union struct {
8+
id []int // 父链接数组(由触点索引)
9+
sz []int // (由触点索引的) 各个根节点所对应的分量的大小
10+
count int // 连通分量的数量
11+
}
12+
13+
func newUnion(N int) *union {
14+
id := make([]int, N)
15+
for i := range id {
16+
id[i] = i
17+
}
18+
sz := make([]int, N)
19+
for i := range sz {
20+
sz[i] = 1
21+
}
22+
return &union{
23+
id: id,
24+
sz: sz,
25+
count: N,
26+
}
27+
}
28+
29+
func (u *union) isConnected(p, q int) bool {
30+
return u.find(p) == u.find(q)
31+
}
32+
33+
func (u *union) find(p int) int {
34+
// 跟随连接找到根节点
35+
for p != u.id[p] {
36+
p = u.id[p]
37+
}
38+
return p
39+
}
40+
41+
func (u *union) union(p, q int) {
42+
i, j := u.find(p), u.find(q)
43+
if i == j {
44+
return
45+
}
46+
if u.sz[i] > u.sz[j] {
47+
i, j = j, i
48+
}
49+
// 将小树的根节点连接到大树的根节点
50+
u.id[i] = j
51+
u.sz[j] += u.sz[i]
52+
u.count--
53+
return
54+
}

0 commit comments

Comments
 (0)