1, surrounded by cells containing 0 or the boundary of the grid.
The goal is to return the maximum area of an island, where the area is the total number of connected cells containing
1. Cells are connected horizontally or vertically, and if the grid contains no island, return 0.
Problem
You are given anm x n matrix grid containing 0 and 1. Find the maximum area of an island:
- An island consists of connected cells containing
1. Cells are connected horizontally or vertically.- The area of an island is the total number of cells containing
1 in that island.- If the grid contains no island, return
0.
Example 1
Input
grid = [
[0,0,1,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,1,1,0,0,0,0,1,1,0,0,0,0],
[0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,1,1,0,0,1,0,0,0],
[0,0,0,0,0,1,1,0,0,1,1,1,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0]
]
Output
6
Example 2
Input
grid = [
[0,0,0,0,0,0,0,0]
]
Output
0
Solution
This problem can be solved using a Depth-First Search (DFS) approach. Instead of calculating the area of every island separately using additional data structures, we traverse the grid and calculate each island's area recursively.The key observation is that every connected group of
1 cells forms an island. Starting from any unvisited land cell, DFS can explore all connected land cells and count them.
First, we traverse every cell in the grid. Whenever we find a cell containing
1, we start a DFS from that cell to calculate the area of its island.
During DFS, we mark each visited land cell by changing
1 to 0. This prevents the same cell from being counted more than once. We then recursively explore all four directions: up, down, left, and right.
The DFS function returns
1 for the current land cell plus the areas returned by its four recursive calls. If a cell is outside the grid or contains 0, the function returns 0.
After calculating an island's area, we update the maximum area using
Math.max(). Once the entire grid has been traversed, the maximum area is returned.
This approach works because each land cell is visited only once. By marking visited cells as
0, we avoid revisiting them and ensure that every island's area is calculated correctly.
class Solution {
public int maxAreaOfIsland(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
int maxArea = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
int area = dfs(grid, i, j);
maxArea = Math.max(maxArea, area);
}
}
}
return maxArea;
}
private int dfs(int[][] grid, int i, int j) {
int m = grid.length;
int n = grid[0].length;
// Stop if out of bounds or water
if (i < 0 || i >= m ||
j < 0 || j >= n ||
grid[i][j] != 1) {
return 0;
}
// Mark this cell as visited
grid[i][j] = 0;
// Count the current cell and explore all four directions
int area = 1;
area += dfs(grid, i + 1, j);
area += dfs(grid, i - 1, j);
area += dfs(grid, i, j + 1);
area += dfs(grid, i, j - 1);
return area;
}
}
Complexity
Letm be the number of rows and n be the number of columns.
We traverse the entire grid and perform DFS whenever we encounter an unvisited land cell. Each cell is visited at most once by DFS, and the grid is traversed once to find islands. Therefore, the overall time complexity is
O(m x n).
The recursive DFS can use up to
O(m x n) space on the call stack in the worst case, when all cells belong to a single connected island. The grid is modified in place, so no additional matrix is required.
Comments
No comments yet. Ask a question or share what helped.