[Leetcode] Max Area of Island 最大岛屿面积

news/2024/7/3 2:30:01

Max Area of Island

最新更新请见:https://yanjia.me/zh/2019/02/...

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.

深度优先搜索

思路

该题基本上就是Number of Islands的变形题,唯一的区别是在Number of Islands中我们只需要将搜索到的陆地置为0,保证其不会再被下次探索所用就行了。但这题多了一要求就是要同时返回岛屿的面积。那么最简单的方式就是在递归的时候,每个搜索到的格子都将自身的面积1,加上四个方向搜索出来的延伸面积都加上,再返回给调用递归的那个格子作为延伸面积使用,这样一直返回到岛屿的起始格子时,面积之和就是岛屿的总面积了。

代码

Go

func maxAreaOfIsland(grid [][]int) int {
    maxArea := 0
    for i := range grid {
        for j := range grid[i] {
            area := measureIsland(grid, i, j)
            if area > maxArea {
                maxArea = area
            }
        }
    }
    return maxArea
}

func measureIsland(grid [][]int, x, y int) int {
    if grid[x][y] == 0 {
        return 0
    }
    area := 1
    grid[x][y] = 0
    if x > 0 {
        area += measureIsland(grid, x-1, y)
    }
    if x < len(grid)-1 {
        area += measureIsland(grid, x+1, y)
    }
    if y > 0 {
        area += measureIsland(grid, x, y-1)
    }
    if y < len(grid[0])-1 {
        area += measureIsland(grid, x, y+1)
    }
    return area
}

http://www.niftyadmin.cn/n/4606981.html

相关文章

Splash的实现

&#xfeff;&#xfeff;什么是Splash Splash也就是应用程序启动之前先启动一个画面&#xff0c;上面简单的介绍应用程序的厂商&#xff0c;厂商的LOGO&#xff0c;名称和版本等信息&#xff0c;多为一张图片&#xff0c;显示几秒钟后会自动消息&#xff0c;然后显示出应用程…

Android 音视频深入 十五 FFmpeg 推流mp4文件(附源码下载)

源码地址https://github.com/979451341/Rtmp 1.配置RTMP服务器 这个我不多说贴两个博客分别是在mac和windows环境上的&#xff0c;大家跟着弄MAC搭建RTMP服务器https://www.jianshu.com/p/6fcec3b9d644这个是在windows上的&#xff0c;RTMP服务器搭建(crtmpserver和nginx) http…

基于PelicanDT实现dubbo断网验证

具体介绍 Dubbo-example&#xff0c;是基于PelicanDT实现dubbo环境准备&#xff0c;禁止端口网络访问&#xff0c;执行接口调用验证端口是否禁用示例 前期准备 本示例程序是基于阿里云ECS或远程Linux服务器完成&#xff0c;只需购买阿里云机器&#xff0c;或者选定已准备好的远…

Launcher解析

&#xfeff;&#xfeff;首先来说说我为什么写这篇文章&#xff0c;最近公司要我负责搞Launcher&#xff0c;网上一查这方面的资料比较少&#xff0c;并且不全&#xff0c;研究起来相当困难&#xff0c;所以就写了这篇文章&#xff0c;希望对大家有帮助。这篇文章是相当长的&a…

你遵守开源协议GPL/LGPL吗?

一款Linux 手机操作系统&#xff0c;至少要有40&#xff5e;50个开源项目的支援。这些开源项目拿来后如何用&#xff1f;直接修改&#xff0c;当然要修改。修改后如何发布&#xff1f;还是闭源算了&#xff01;这个问题我也考虑过&#xff0c;有时又不想考虑。 下面是网友的关于…

滴滴工程师带你深入理解 TCP 握手分手全过程

本文作者&#xff1a;饶全成&#xff0c;中科院计算所硕士&#xff0c;滴滴出行后端研发工程师。个人主页&#xff1a;https://zhihu.com/people/raoquancheng记得刚毕业找工作面试的时候&#xff0c;经常会被问到&#xff1a;你知道“3次握手&#xff0c;4次挥手”吗&#xff…

Linux手机开发中,尽量不要用多线程。

在Linux手机操作系统中&#xff0c;一般不提倡用多线程&#xff0c;为什么呢&#xff1f; 1 难调试&#xff1b; 2 难同步。 所以&#xff0c;一个进程中就搞一个线程。不要在进程中搞一堆线程&#xff0c;否则调试起来很痛苦。 不过也有一些比较特殊的程序&#xff0c;比如…

精辟的山寨。

有人为山寨机编了这么一段话&#xff1a; “一定得选最好的硬件芯片&#xff0c;雇法国设计师&#xff0c;做就得做最高档的手机&#xff1b; 平台直接用MTK&#xff0c;屏幕最小也得3.0的&#xff0c;什么智能呀、电视功能呀、双卡同时待机呀、能给他装的全给他装上&#xf…