695. Max Area of Island最大岛屿面积

[抄题]:

求最多的联通的1的数量

Given a non-empty 2D array grid of 0's and 1's, 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.

Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)

Example 1:

[[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]]

Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.

Example 2:

[[0,0,0,0,0,0,0,0]]

Given the above grid, return 0.

 [暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

  1. 以为棋盘问题都是向四周扩展、bfs,其实本质上不是对棋盘元素操作,本质上是求数量最大,还是DFS先求所有
  2. 中居然也能用二叉树的traverse嵌套,头一次见

[一句话思路]:

某点的面积是由四周的点构成的,四周的点的面积又是由四周的点构成的,所以用traverse递归嵌套。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

为防止重复计算,把1的点先标记为0,使其不再符合条件。第一次见。

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

  [五分钟肉眼debug的结果]:

没看出来把= 写成 == 了,不应该

[总结]:

本质上不是对棋盘元素操作,本质上是求数量最大,还是DFS先求所有

[复杂度]:Time complexity: O(n2) Space complexity: O(n2)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

本质上不是对棋盘元素操作,本质上是求数量最大,还是DFS先求所有

[关键模板化代码]:

public int areaOfIsland(int i, int j, int[][] grid) {
        //valid first, == 1 second
    if (0 <= i && i < grid.length && 0<= j && j < grid[0].length && grid[i][j] == 1) {
        //restore to 0 to avoid repeat
        grid[i][j] = 0;
        //count area
        return 1 + areaOfIsland(i - 1, j, grid) + areaOfIsland(i + 1, j, grid) + areaOfIsland(i, j - 1, grid) + areaOfIsland(i, j + 1, grid);
    }
        //if not 1, default case : return 0
        return 0;
    }
traverse嵌套

[其他解法]:

并查集,太麻烦了

[Follow Up]:

[LC给出的题目变变变]:

 [代码风格] :

class Solution {
    public int maxAreaOfIsland(int[][] grid) {
        //corner case
        if (grid.length == 0 || grid[0].length == 0) {
            return 0;
        }
        //compare all areas
        int max = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                max = Math.max(max, areaOfIsland(i, j, grid));
            }
        }
        return max;
    }
    
    public int areaOfIsland(int i, int j, int[][] grid) {
        //valid first, == 1 second
    if (0 <= i && i < grid.length && 0<= j && j < grid[0].length && grid[i][j] == 1) {
        //restore to 0 to avoid repeat
        grid[i][j] = 0;
        //count area
        return 1 + areaOfIsland(i - 1, j, grid) + areaOfIsland(i + 1, j, grid) + areaOfIsland(i, j - 1, grid) + areaOfIsland(i, j + 1, grid);
    }
        //if not 1, default case : return 0
        return 0;
    }
}
View Code

 

转载于:https://www.cnblogs.com/immiao0319/p/8546125.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值