Skip to content

Commit 41b3df4

Browse files
create file
1 parent 2e6cfcf commit 41b3df4

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class MaxAreaOfIsland {
2+
static int n;
3+
static int m;
4+
public int maxArea(int[][] grid) {
5+
n = grid.length;
6+
m = grid[0].length;
7+
8+
int maxArea = 0;
9+
10+
for(int i = 0; i<n; i++){
11+
for(int j = 0; j<m; j++){
12+
if(grid[i][j] == 1){
13+
int count = dfs(grid, i, j);
14+
maxArea = Math.max(maxArea, count);
15+
}
16+
}
17+
}
18+
19+
return maxArea;
20+
}
21+
22+
static int dfs(int[][] grid, int i, int j){
23+
if(i<0 || j<0 || i>n-1 || j>m-1 || grid[i][j] == 0){
24+
return 0;
25+
}
26+
27+
grid[i][j] = 0;
28+
29+
return dfs(grid, i-1, j)+dfs(grid, i+1, j)+dfs(grid, i, j-1)+dfs(grid, i, j+1)+1;
30+
}
31+
}

0 commit comments

Comments
 (0)