Skip to content

Commit

Permalink
GO: 695. Max Area of Island
Browse files Browse the repository at this point in the history
  • Loading branch information
MaratKhakim committed Aug 20, 2022
1 parent c44c74f commit 7180f58
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions go/695-Max-Area-Of-Island.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
func maxAreaOfIsland(grid [][]int) int {
row, col := len(grid), len(grid[0])
res := 0

for i := 0; i < row; i++ {
for j := 0; j < col; j++ {
if grid[i][j] == 1 {
curr := dfs(grid, i, j)
if curr > res {
res = curr
}
}
}
}

return res
}

func dfs(grid [][]int, row, col int) int {
if row < 0 || row >= len(grid) || col < 0 || col >= len(grid[0]) {
return 0
}

if grid[row][col] == 0 {
return 0
}

grid[row][col] = 0

return dfs(grid, row+1, col) + dfs(grid, row-1, col) + dfs(grid, row, col+1) + dfs(grid, row, col-1) + 1
}

0 comments on commit 7180f58

Please sign in to comment.