-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsolution.go
45 lines (41 loc) · 987 Bytes
/
solution.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
package main
import "fmt"
func maxAreaOfIsland(grid [][]int) int {
var max int
for i := 0; i < len(grid); i++ {
for j := 0; j < len(grid[i]); j++ {
if grid[i][j] == 1 {
result := dfs(grid, i, j)
if result > max {
max = result
}
}
}
}
return max
}
func dfs(grid [][]int, i, j int) int {
if i < 0 || i >= len(grid) || j < 0 || j >= len(grid[i]) || grid[i][j] == 0 {
return 0
}
grid[i][j] = 0
count := 1
count += dfs(grid, i+1, j)
count += dfs(grid, i-1, j)
count += dfs(grid, i, j+1)
count += dfs(grid, i, j-1)
return count
}
func main() {
grid := [][]int{
{0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
{0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0},
{0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0},
}
fmt.Println(maxAreaOfIsland(grid))
}