forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0695-max-area-of-island.cs
28 lines (25 loc) · 1.02 KB
/
0695-max-area-of-island.cs
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
public class Solution {
public int MaxAreaOfIsland(int[][] grid)
{
int r = grid.Length, c = grid[0].Length, area = 0;
bool[,] visits = new bool[r, c];
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
area = Math.Max(area, DFSMaxAreaOfIsland(i, j, grid, visits));
}
}
return area;
}
private int DFSMaxAreaOfIsland(int row, int col, int[][] grid, bool[,] visits)
{
int m = grid.Length, n = grid[0].Length;
if (row < 0 || row >= m || col < 0 || col >= n || visits[row, col] || grid[row][col] == 0)
return 0;
visits[row, col] = true;
return (1 + DFSMaxAreaOfIsland(row, col + 1, grid, visits) +
DFSMaxAreaOfIsland(row, col - 1, grid, visits) +
DFSMaxAreaOfIsland(row + 1, col, grid, visits) +
DFSMaxAreaOfIsland(row - 1, col, grid, visits));
}}