LeetCode695. Max Area of Island

2023-12-01 10:28
文章标签 max area leetcode695 island

本文主要是介绍LeetCode695. 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:

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.

二、题解

class Solution {
public:int count = 1;int dirs[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};void dfs(vector<vector<int>>& grid,vector<vector<bool>>& visted,int x,int y){for(int i = 0;i < 4;i++){int nextX = x + dirs[i][0];int nextY = y + dirs[i][1];if(nextX < 0 || nextX >= grid.size() || nextY < 0 || nextY >= grid[0].size()) continue;if(grid[nextX][nextY] == 1 && visted[nextX][nextY] == false){count++;visted[nextX][nextY] = true;dfs(grid,visted,nextX,nextY);}}}int maxAreaOfIsland(vector<vector<int>>& grid) {int m = grid.size();int n = grid[0].size();int res = 0;vector<vector<bool>> visted(m,vector<bool>(n,false));for(int i = 0;i < m;i++){for(int j = 0;j < n;j++){if(grid[i][j] == 1 && visted[i][j] == false){count = 1;visted[i][j] = true;dfs(grid,visted,i,j);res = max(res,count);}}}return res;}
};

这篇关于LeetCode695. Max Area of Island的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/440807

相关文章

file-max与ulimit的关系与差别

http://zhangxugg-163-com.iteye.com/blog/1108402 http://ilikedo.iteye.com/blog/1554822

POJ 1050 To the Max(枚举+动规)

题目: http://poj.org/problem?id=1050 题解: 此题转化成一维后就相当于求最大连续子序列了,可以枚举所有的行组合,把枚举到的起始行到终止行的值按列相加存入一个一维数组。 代码: #include<cstdio>#include<cstring>int a[101][101];int value[101];int dp[101];int max(

报错:Reached the max session limit(DM8 达梦数据库)

报错:Reached the max session limit - - DM8 达梦数据库 1 环境介绍2 数据库启动SYSTEM IS READY后面日志3 数据库刚启动日志4 达梦数据库学习使用列表 1 环境介绍 某项目无法连接数据库,报错:超过最大会话数限制 , 检查 dmdba ulimit -a openfiles 已改检查 dm.ini 其中 MAX_SESSION

【硬刚ES】ES基础(二十) 单字符串多字段查询:Dis Max Query

本文是对《【硬刚大数据之学习路线篇】从零到大数据专家的学习指南(全面升级版)》的ES部分补充。

[LeetCode] 695. Max Area of Island

题:https://leetcode.com/problems/max-area-of-island/description/ 题目 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 (horizont

[LeetCode] 485. Max Consecutive Ones

题: 题目 Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three digits are consec

whose UTF8 encoding is longer than the max length 32766

问题描述:java.lang.IllegalArgumentException: Document contains at least one immense term in field=“cf_jg.keyword” (whose UTF8 encoding is longer than the max length 32766) 原因:设置为keyword类型的字段,插入很长的大段内容后,报

HDU_Max Sum(DP)

解题报告 http://blog.csdn.net/juncoder/article/details/38150533 题目传送门 题意: 求子区间连续和最大 思路: DP,dp[i]=max(dp[i-1]+num[i],num[i]) 如果区间内有一个数使得连续和小于0,那么从那个数开始重新定位区间。 #include <cstdio>#include <cstring

收好了,这些max工作效率的学习资源,赶紧用起来

点击上方“朱小厮的博客”,选择“设为星标” 回复”1024“获取独家整理的学习资料 现在,技术已经成为所有行业创新的基石。 这引发全世界对程序员和开发人员的巨大需求。 技术的美妙之处在于它触手可及,所以... 你不用先成为火箭科学家就能成为一个牛逼的程序员。 这看起来可能很难,但事实是......它可能比你想象的要容易。 你只需要阅读,练习并付出一些努力。 但是......这不就是你每天都

Truncated incorrect max_connections value: ‘999999‘

MySQL 的最大连接数(max_connections)可以设置的上限值在不同的资料中有所不同。以下是一些关键信息: 默认值和默认范围: MySQL 的默认最大连接数通常为 100 。一些资料提到默认值为 151 。 最大允许值: MySQL 的最大连接数上限通常为 16384 。有些情况下,最大连接数可以设置为 10000 。 设置方法: 可以通过命令行临时设置,例如 set gl