-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxAreaOfIsland.java
More file actions
34 lines (27 loc) · 1.31 KB
/
MaxAreaOfIsland.java
File metadata and controls
34 lines (27 loc) · 1.31 KB
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
29
30
31
32
33
34
/*
You are given an m x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
The area of an island is the number of cells with a value 1 in the island.
Return the maximum area of an island in grid. If there is no island, return 0.
*/
class Solution {
public int maxAreaOfIsland(int[][] grid) {
boolean visited[][] = new boolean[grid.length][grid[0].length];
int maxArea = 0;
for(int i = 0;i < grid.length;i++){
for(int j = 0;j < grid[0].length;j++)
if(grid[i][j] == 1 && visited[i][j] == false){
int currArea = dfs(grid, visited, i, j, grid.length, grid[0].length);
// save max area
maxArea = Math.max(maxArea, currArea);
}
}
return maxArea;
}
private int dfs(int[][] grid, boolean[][] v, int i, int j, int row, int col){
if(i < 0 || i >= row || j < 0 || j >= col || grid[i][j] == 0 || v[i][j] == true)
return 0;
v[i][j] = true;
return 1 + dfs(grid, v, i, j+1, row, col) + dfs(grid, v, i, j - 1, row, col) +
dfs(grid, v, i-1, j, row, col) + dfs(grid, v, i+1, j, row, col);
}
}