Skip to content

Commit 45ac778

Browse files
authored
added java file
1 parent bda2033 commit 45ac778

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

MaxAreaOfIsland.java

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

0 commit comments

Comments
 (0)