forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0695.go
37 lines (33 loc) · 740 Bytes
/
0695.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
var r, c int
var d []int
func maxAreaOfIsland(grid [][]int) int {
r, c = len(grid), len(grid[0])
d = []int{0, 1, 0, -1, 0}
res := 0
for i := 0; i < r; i++ {
for j := 0; j < c; j++ {
if grid[i][j] == 1 {
grid[i][j] = 0
res = max(res, dfs(grid, i, j))
}
}
}
return res
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func dfs(grid [][]int, x, y int) int {
res := 1
for i := 0; i < 4; i++ {
nx, ny := x + d[i], y + d[i + 1]
if 0 <= nx && nx < r && 0 <= ny && ny < c && grid[nx][ny] == 1 {
grid[nx][ny] = 0
res += dfs(grid, nx, ny)
}
}
return res
}