-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxAreaOfIsland.java
More file actions
23 lines (21 loc) · 863 Bytes
/
maxAreaOfIsland.java
File metadata and controls
23 lines (21 loc) · 863 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class maxAreaOfIsland{
public static void main(String[] args){
int grid[][]={{1,1,0,0,0},{1,1,0,0,0},{0,0,0,1,1},{0,0,0,1,1}};
int area=max_areaofisland(grid);
System.out.println(area);
}
public static int max_areaofisland(int[][] grid) {
int max_area = 0;
for(int i = 0; i < grid.length; i++)
for(int j = 0; j < grid[0].length; j++)
if(grid[i][j] == 1)max_area = Math.max(max_area, AreaOfIsland(grid, i, j));
return max_area;
}
public static int AreaOfIsland(int[][] grid, int i, int j){
if( i >= 0 && i < grid.length && j >= 0 && j < grid[0].length && grid[i][j] == 1){
grid[i][j] = 0;
return 1 + AreaOfIsland(grid, i+1, j) + AreaOfIsland(grid, i-1, j) + AreaOfIsland(grid, i, j-1) + AreaOfIsland(grid, i, j+1);
}
return 0;
}
}