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

Commit e944534

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

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ func largestComponentSize(A []int) int {
1111
for i := 0; i < size-1; i++ {
1212
for j := i + 1; j < size; j++ {
1313
a, b := A[i], A[j]
14-
if u.isConnected(i, j) || gcd(a, b) > 1 {
15-
u.union(i, j)
14+
m, n := u.find(i), u.find(j)
15+
if m == n || gcd(a, b) == 1 {
16+
continue
1617
}
18+
u.union(m, n)
1719
}
1820
}
1921

@@ -68,11 +70,7 @@ func (u *union) find(p int) int {
6870
return p
6971
}
7072

71-
func (u *union) union(p, q int) {
72-
i, j := u.find(p), u.find(q)
73-
if i == j {
74-
return
75-
}
73+
func (u *union) union(i, j int) {
7674
if u.sz[i] > u.sz[j] {
7775
i, j = j, i
7876
}

0 commit comments

Comments
 (0)