LeetCode 695. Max Area of Island (岛的最大区域)

时间:2022-04-27 22:52:18

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.

Note: The length of each dimension in the given grid does not exceed 50.


题目标签:Array

  题目给了我们一个 2d grid array, 让我们找到所有岛中区域最大的一个,返回区域值。0代表海洋,1代表陆地。陆地与陆地相连,只能是横向和纵向,不可以斜着。

  因为只能横向和纵向相连,所以每一个cell 只能是4个方向延伸,左 上 右 下。

  这道题目要用到Depth-first Search,遍历2d array,遇到1的时候,就利用dfs把这个岛的区域大小找全。我的dps顺序是 左,上,右,下。在递归dfs之前,要把目前的cell

  设为0,是为了避免dfs又往回走,每一个数过的cell,就不需要在重复走了。

  题外话:最近因为看不了极限挑战,所以这几天看了东方卫视的另一个节目 <梦想改造家4> , 挺好看的,特别是第4集。各位程序猿休息的时候可以看看!谁都不是一座孤岛!加油刷题!

Java Solution:

Runtime beats 53.35%

完成日期:10/22/2017

关键词:Array

关键点:DFS

 class Solution
{
public int maxAreaOfIsland(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, dfs(grid, i, j));
}
} return max_area;
} public int dfs(int[][] grid, int i, int j)
{
// if i or j is invalid or grid is 0, just return 0
if( i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == 0)
return 0; // do dfs to its 4 direction cell when value is 1
int tempMaxArea = 1;
grid[i][j] = 0; // set current cell to 0 to prevent dfs coming back // order is left, top, right, bottom
tempMaxArea += dfs(grid, i, j-1) + dfs(grid, i-1, j) + dfs(grid, i, j+1) + dfs(grid, i+1, j); return tempMaxArea;
}
}

参考资料:

https://discuss.leetcode.com/topic/106301/java-c-straightforward-dfs-solution

LeetCode 题目列表 - LeetCode Questions List

LeetCode 695. Max Area of Island (岛的最大区域)的更多相关文章

  1. leetcode 695 Max Area of Island 岛的最大面积

    这个题使用深度优先搜索就可以直接遍历 DFS递归方法: class Solution { public: vector<vector<,},{,-},{,},{,}}; int maxAr ...

  2. &lbrack;Leetcode&rsqb;695&period; Max Area of Island

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  3. leetcode 200&period; Number of Islands 、694&Tab; Number of Distinct Islands 、695&period; Max Area of Island 、130&period; Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  4. &lbrack;leetcode&rsqb;python 695&period; Max Area of Island

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  5. 【leetcode】Max Area of Island

    国庆中秋长假过完,又要开始上班啦.先刷个题目找找工作状态. Given a non-empty 2D array grid of 0's and 1's, an island is a group o ...

  6. &lbrack;LeetCode&rsqb; Max Area of Island 岛的最大面积

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  7. 【LeetCode】695&period; Max Area of Island 解题报告(Python & C&plus;&plus;)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

  8. 200&period; Number of Islands &plus; 695&period; Max Area of Island

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. 【easy】695&period; Max Area of Island

    题目: Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) ...

随机推荐

  1. &commat;Scheduled 注解

    Spring配置文件xmlns加入 <!---加入:xmlns:task="http://www.springframework.org/schema/task"--> ...

  2. 看ImplicitBackwardEulerSparse关于static solve的代码

    当选择static solve的时候,求解的流程如下: 1.获得内力 2.qresidual = 外力-内力,qdelta = qresidual, qdelta的非约束元素赋给bufferConst ...

  3. c&num; in depth之泛型的类型约束详细

    类型约束 1.引用类型约束 这种约束(表示成T:class,必须是为类型参数指定的第一个约束)用于确保使用的类型实参是引用类型,这可能是任何类,接口,数组,委托或者已知是引用类型的另一个类型参数. 例 ...

  4. ODOO&lpar;ERP源码安装&rpar;

    cat /etc/centos-release CentOS Linux release 7.4.1708 (Core) uname -r 3.10.0-693.el7.x86_64 IP:192.1 ...

  5. Java中Date, Calendar, SimpleDateFormat的相互转换

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; impor ...

  6. &lbrack;Mac&rsqb;secureCRT私钥转换为mac ssh私钥

    工作环境从win迁移到mac后,win上原来用secureCRT生成的key,在mac的iterm2中不能兼容使用,导致无法再mac下登录.报错如下: key_load_public:invalid ...

  7. Java伪代码之大道至简读后感

    import.java.大道至简.*; import.java. 愚公移山.*; public class YuGongYiShan//定义一个名为YuGongYiShan的类 {//类定义的开始 S ...

  8. 采用redis 主从架构的原因

    如果系统的QPS超过10W+,甚至是百万以上的访问,则光是Redis是不够的,但是Redis是整个大型缓存架构中,支撑高并发的架构非常重要的环节. 首先,你的缓存中间件.缓存系统,必须能够支撑起10w ...

  9. POJ 1113 Wall 凸包 裸

    LINK 题意:给出一个简单几何,问与其边距离长为L的几何图形的周长. 思路:求一个几何图形的最小外接几何,就是求凸包,距离为L相当于再多增加上一个圆的周长(因为只有四个角).看了黑书使用graham ...

  10. Hadoop1的安装

    目前hadoop1的稳定版本是1.2.1,我们以版本1.2.1为例详细的介绍hadoop1的安装,此过程包括OS安装与配置,JDK的安装,用户和组的配置,这些过程在hadoop2也有可能用到. Had ...