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

Commit 22ed933

Browse files
committed
952 Time Limit Exceeded
1 parent a1f498c commit 22ed933

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package problem0952
22

3+
import "sort"
4+
35
func largestComponentSize(A []int) int {
46
size := len(A)
57
u := newUnion(size)
68

9+
sort.Ints(A)
10+
711
for i := 0; i < size-1; i++ {
812
for j := i + 1; j < size; j++ {
913
a, b := A[i], A[j]
10-
if gcd(a, b) > 1 {
14+
if u.isConnected(i, j) || gcd(a, b) > 1 {
1115
u.union(i, j)
1216
}
1317
}
@@ -33,7 +37,7 @@ func gcd(a, b int) int {
3337
type union struct {
3438
id []int // 父链接数组(由触点索引)
3539
sz []int // (由触点索引的) 各个根节点所对应的分量的大小
36-
max int // 连通分量的数量
40+
max int
3741
}
3842

3943
func newUnion(N int) *union {

0 commit comments

Comments
 (0)