-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber-of-provinces.go
50 lines (42 loc) · 986 Bytes
/
number-of-provinces.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import "fmt"
// source: https://leetcode.com/problems/number-of-provinces/
func findCircleNum(isConnected [][]int) int {
var res int = len(isConnected)
parent := make([]int, len(isConnected))
var find func(int) int
var union func(int, int)
union = func(x, y int) {
fx, fy := find(x), find(y)
parent[fx] = fy
if fx != fy {
res--
}
}
find = func(x int) int {
if parent[x] != x {
parent[x] = find(parent[x])
}
return parent[x]
}
N := len(isConnected)
for i := 0; i < N; i++ {
parent[i] = i
}
for i := 0; i < N; i++ {
for j := i + 1; j < len(isConnected); j++ {
if isConnected[i][j] == 1 {
union(i, j)
}
}
}
return res
}
func main() {
// Example 1
var isConnected1 = [][]int{{1, 1, 0}, {1, 1, 0}, {0, 0, 1}}
fmt.Println("Expected: 2 Output: ", findCircleNum(isConnected1))
// Example 2
var isConnected2 = [][]int{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}
fmt.Println("Expected: 3 Output: ", findCircleNum(isConnected2))
}