当前位置:网站首页>LeetCode_ DFS_ Medium_ 695. Maximum area of the island
LeetCode_ DFS_ Medium_ 695. Maximum area of the island
2022-04-23 13:11:00 【A scoop of Jianghu me ups and downs】
1. subject
Give you a size of m x n The binary matrix of grid .
Islands It's made up of some neighboring 1 ( Representing land ) A combination of components , there 「 adjacent 」 Ask for two 1 Must be in In four horizontal or vertical directions adjacent . You can assume grid The four edges of are all 0( Representative water ) Surrounded by a .
The area of the island is the value of 1 Number of cells .
Calculate and return grid The largest island area in . If there were no Islands , Then the return area is 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,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
explain : The answer should not be 11 , Because the island can only contain horizontal or vertical 1 .
Example 2:
Input :grid = [[0,0,0,0,0,0,0,0]]
Output :0
Tips :
m == grid.length
n == grid[i].length
1 <= m, n <= 50
grid[i][j] by 0 or 1
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/max-area-of-island
2. Ideas
(1)DFS
This question is actually LeetCode_DFS_ secondary _200. Number of Islands This deformation problem , It's just dfs Function submerges the island at the same time , To record the area of the island .
3. Code implementation (Java)
// Ideas 1————DFS
class Solution {
public int maxAreaOfIsland(int[][] grid) {
//res Record the maximum area of the island , The initial value is 0
int res = 0;
int m = grid.length;
int n = grid[0].length;
// Traverse grid
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
// Submerge the island , And update res
res = Math.max(res, dfs(grid, i, j));
}
}
}
return res;
}
// Drown and (i, j) Adjacent land , And return to the flooded land area
public int dfs(int[][] grid, int i, int j) {
int m = grid.length;
int n = grid[0].length;
// Check the boundary conditions
if (i < 0 || j < 0 || i >= m || j >= n) {
return 0;
}
if (grid[i][j] == 0) {
// This location is sea water , Go straight back to
return 0;
}
// take (i, j) Turn into sea water
grid[i][j] = 0;
return dfs(grid, i - 1, j) +
dfs(grid, i + 1, j) +
dfs(grid, i, j - 1) +
dfs(grid, i, j + 1) + 1;
}
}
版权声明
本文为[A scoop of Jianghu me ups and downs]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231307045762.html
边栏推荐
- web三大组件之Filter、Listener
- 2020最新Android大厂高频面试题解析大全(BAT TMD JD 小米)
- PC starts multiple wechat at one time
- 超40W奖金池等你来战!第二届“长沙银行杯”腾讯云启创新大赛火热来袭!
- 【快排】215. 数组中的第K个最大元素
- (1) Openjuterpyrab comparison scheme
- Golang implements a five insurance and one gold calculator with web interface
- 在 pytorch 中加载和使用图像分类数据集 Fashion-MNIST
- 1130 - host XXX is not allowed to connect to this MySQL server error in Navicat remote connection database
- [51 single chip microcomputer traffic light simulation]
猜你喜欢

Design and manufacture of 51 single chip microcomputer solar charging treasure with low voltage alarm (complete code data)

AUTOSAR from introduction to mastery lecture 100 (84) - Summary of UDS time parameters

C语言之字符串与字符数组的区别
![[wechat applet] flex layout usage record](/img/ab/7b2392688d8a0130e671f179e09dce.png)
[wechat applet] flex layout usage record

数据仓库—什么是OLAP

Important knowledge of network layer (interview, reexamination, term end)

Mui + hbuilder + h5api simulate pop-up payment style

Use Proteus to simulate STM32 ultrasonic srf04 ranging! Code+Proteus

解决Oracle中文乱码的问题

100 lectures on practical application cases of Excel (VIII) - report connection function of Excel
随机推荐
Important knowledge of network layer (interview, reexamination, term end)
Go language slicing operation
ESP32 VHCI架构传统蓝牙设置scan mode,让设备能被搜索到
初鉴canvas,展示个小小的小案例
JDBC connection pool
SSM整合之pom.xml
AUTOSAR from introduction to mastery 100 lectures (83) - bootloader self refresh
Navicat远程连接数据库 出现 1130- Host xxx is not allowed to connect to this MySQL server错误
Connect orcale
CMSIS cm3 source code annotation
Mui wechat payment pit
HQL statement tuning
office2021安装包下载与激活教程
The first lesson is canvas, showing a small case
100 lectures on practical application cases of Excel (VIII) - report connection function of Excel
AUTOSAR from introduction to mastery 100 lectures (51) - AUTOSAR network management
R语言中dcast 和 melt的使用 简单易懂
PC starts multiple wechat at one time
解决虚拟机中Oracle每次要设置ip的问题
(个人)最近项目开发后存在的系统漏洞整理