-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount-sub-islands.go
166 lines (144 loc) · 4.74 KB
/
count-sub-islands.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"fmt"
)
type pair struct {
x, y int
}
func countSubIslands(grid1 [][]int, grid2 [][]int) int {
n, m := len(grid1), len(grid1[0])
// union-find on both islands
ds := make(map[pair]pair, n*m)
var find func(pair) pair
var union func(pair, pair)
union = func(x, y pair) {
ds[find(x)] = find(y)
}
find = func(x pair) pair {
if ds[x] != x {
ds[x] = find(ds[x])
}
return ds[x]
}
for i := range grid2 {
for j := range grid2[0] {
ds[pair{i, j}] = pair{i, j}
}
}
dirs := [][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
// union islands
for i := range grid2 {
for j := range grid2[0] {
if grid2[i][j] == 1 {
for _, dir := range dirs {
if x, y := i+dir[0], j+dir[1]; x >= 0 && x < n && y >= 0 && y < m && grid2[x][y] == 1 {
union(pair{i, j}, pair{x, y})
}
}
}
}
}
invalidIslands := make(map[pair]bool)
for i := range grid2 {
for j := range grid2[0] {
if grid1[i][j] == 0 && grid2[i][j] == 1 {
invalidIslands[find(pair{i, j})] = true
}
}
}
// count valid islands
ans := 0
for i := range grid2 {
for j := range grid2[0] {
if grid2[i][j] == 1 && !invalidIslands[find(pair{i, j})] {
ans++
invalidIslands[find(pair{i, j})] = true
}
}
}
return ans
}
/*func countSubIslands(grid1 [][]int, grid2 [][]int) int {
n, m := len(grid1), len(grid1[0])
dirs := [][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
// calculate
var ans int
var checkIsland func(i, j int)
checkIsland = func(i, j int) {
valid := !(grid1[i][j] == 0 && grid2[i][j] == 1)
if grid1[i][j] == 1 {
grid1[i][j] = 2
}
for _, dir := range dirs {
if x, y := i+dir[0], j+dir[1]; x >= 0 && x < n && y >= 0 && y < m {
//println(grid1[x][y], grid1[x][y] == 1)
if grid1[x][y] == 1 {
grid1[x][y] = 2
valid = valid && checkIsland(x, y)
}
if grid1[x][y] == 0 && grid2[x][y] == 1 {
return false
}
}
}
return valid
}
for i := range grid1 {
for j := range grid1[0] {
if grid1[i][j] == 1 {
if checkIsland(i, j) {
ans++
}
}
}
}
return ans
}*/
func main() {
testCases := []struct {
grid1 [][]int
grid2 [][]int
want int
}{
{
grid1: [][]int{{1, 1, 1, 0, 0}, {0, 1, 1, 1, 1}, {0, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, {1, 1, 0, 1, 1}},
grid2: [][]int{{1, 1, 1, 0, 0}, {0, 0, 1, 1, 1}, {0, 1, 0, 0, 0}, {1, 0, 1, 1, 0}, {0, 1, 0, 1, 0}},
want: 3,
},
{
grid1: [][]int{{1, 0, 1, 0, 1}, {1, 1, 1, 1, 1}, {0, 0, 0, 0, 0}, {1, 1, 1, 1, 1}, {1, 0, 1, 0, 1}},
grid2: [][]int{{0, 0, 0, 0, 0}, {1, 1, 1, 1, 1}, {0, 1, 0, 1, 0}, {0, 1, 0, 1, 0}, {1, 0, 0, 0, 1}},
want: 2,
},
{
grid1: [][]int{{1, 1}, {0, 0}},
grid2: [][]int{{0, 1}, {0, 1}},
want: 0,
},
{
grid1: [][]int{{1, 1, 1, 1, 0, 0}, {1, 1, 0, 1, 0, 0}, {1, 0, 0, 1, 1, 1}, {1, 1, 1, 0, 0, 1}, {1, 1, 1, 1, 1, 0}, {1, 0, 1, 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1, 1, 0, 0, 1, 0, 1, 0, 1}, {0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0}, {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1}, {1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1}, {0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1}, {1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1}, {1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0}, {0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0}},
want: 6,
},
}
successes := 0
for _, tc := range testCases {
x := countSubIslands(tc.grid1, tc.grid2)
status := "ERROR"
if fmt.Sprint(x) == fmt.Sprint(tc.want) {
status = "OK"
successes++
}
fmt.Println(status, " Expected: ", tc.want, " Actual: ", x)
}
if l := len(testCases); successes == len(testCases) {
fmt.Printf("===\nSUCCESS: %d of %d tests ended successfully\n", successes, l)
} else {
fmt.Printf("===\nFAIL: %d tests failed\n", l-successes)
}
}