[LeetCode] 695. Max Area of Island

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.

Example 1:
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,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
Output: 6
Explanation: The answer is not 11, because the island must be connected 4-directionally.

Example 2:
Input: grid = [[0,0,0,0,0,0,0,0]]
Output: 0

Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 50
grid[i][j] is either 0 or 1.

岛屿的最大面积。

给你一个大小为 m x n 的二进制矩阵 grid 。

岛屿 是由一些相邻的 1 (代表土地) 构成的组合,这里的「相邻」要求两个 1 必须在 水平或者竖直的四个方向上 相邻。你可以假设 grid 的四个边缘都被 0(代表水)包围着。

岛屿的面积是岛上值为 1 的单元格的数目。

计算并返回 grid 中最大的岛屿面积。如果没有岛屿,则返回面积为 0 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/max-area-of-island
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

这是一道 flood fill 类型的题。我这里还是给出 DFS 和 BFS 两种做法。

无论是那种做法,search 一开始找的是值为 1 的坐标,这没有什么疑问。但是找到 1 之后,则需要把这个 1 当做一个岛的起点开始做 dfs。注意以下几点,同时也是 DFS 模板题都需要注意的

  • 碰到边界就 return 0
  • 把当前坐标的值 mark 成 0(或者一个极值,看情况而定,这是为了防止重复访问造成死循环)
  • 四个方向递归调用 DFS 函数

这个题唯一多的一个步骤就是当找到第一个 1 的时候,需要把递归调用里面所有能找到的岛屿面积累加起来。

复杂度

时间O(mn)
空间O(n)

DFS代码

Java实现

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
class Solution {
public int maxAreaOfIsland(int[][] grid) {
int res = 0;
int m = grid.length;
int n = grid[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
res = Math.max(res, dfs(i, j, grid));
}
}
}
return res;
}

private int dfs(int i, int j, int[][] grid) {
if (i < 0 || j < 0 || i >= grid.length || j >= grid[i].length || grid[i][j] == 0) {
return 0;
}
grid[i][j] = 0;
int num = 1;
num += dfs(i + 1, j, grid);
num += dfs(i - 1, j, grid);
num += dfs(i, j + 1, grid);
num += dfs(i, j - 1, grid);
return num;
}
}

BFS代码

Java实现

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
35
36
37
38
39
class Solution {
int[][] dirs = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
int m;
int n;

public int maxAreaOfIsland(int[][] grid) {
int res = 0;
m = grid.length;
n = grid[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
res = Math.max(res, helper(grid, i, j));
}
}
}
return res;
}

private int helper(int[][] grid, int i, int j) {
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[] { i, j });
grid[i][j] = 0;
int count = 1;
while (!queue.isEmpty()) {
int[] cur = queue.poll();
for (int[] dir : dirs) {
int x = cur[0] + dir[0];
int y = cur[1] + dir[1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) {
queue.offer(new int[] { x, y });
grid[x][y] = 0;
count++;
}
}
}
return count;
}
}

相关题目

1
2
695. Max Area of Island
2658. Maximum Number of Fish in a Grid

[LeetCode] 695. Max Area of Island
https://shurui91.github.io/posts/988496489.html
Author
Aaron Liu
Posted on
April 16, 2020
Licensed under