-
Notifications
You must be signed in to change notification settings - Fork 481
/
0827.go
64 lines (58 loc) · 1.34 KB
/
0827.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
var dire [][]int
func largestIsland(g [][]int) int {
dire = [][]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}
data := []int{0, 0}
r, c, res := len(g), len(g[0]), 0
for i := 0; i < r; i++ {
for j := 0; j < c; j++ {
if g[i][j] == 1 {
data = append(data, dfs(i, j, len(data), g))
}
}
}
for i := 0; i < r; i++ {
for j := 0; j < c; j++ {
if g[i][j] != 0 {
continue
}
s := make(map[int]int)
tmp := 0
for _, d := range dire {
color := check(i + d[0], j + d[1], g)
if _, ok := s[color]; ok {
continue
}
s[color] = 1
tmp += data[color]
}
res = max(res, tmp + 1)
}
}
if res == 0 {
return r * c
}
return res
}
func check(i, j int, g [][]int) int {
if i < 0 || j < 0 || i >= len(g) || j >= len(g[0]) {
return 0
}
return g[i][j]
}
func dfs(i, j, color int, g [][]int) int {
if check(i, j, g) != 1 {
return 0
}
g[i][j] = color
res := 1
for _, d := range dire {
res += dfs(i + d[0], j + d[1], color, g)
}
return res
}
func max(a, b int) int {
if a > b {
return a
}
return b
}