[LeetCode] 695. Max Area of Island_Easy tag: DFS/BFS

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.

这个题目思路就是BFS/DFS.

1. Constraints

1) edge case len(grid), len(grid[0]) == 0 => 0

2)element will be 1, or 0

2. Ideas

DFS    T: O(m*n)     S; O(m*n)

3. Code

class Solution:
    def maxAreaLand(self, grid):
        if not grid or len(grid[0]) == 0: return 0
        lr, lc, ans, visited = len(grid), len(grid[0]), 0, set()
        def dfs(r,c):
            if 0 <= r <len(grid) and 0 <= c < len(grid[0]) and grid[r][c] and (r,c) not in visted:
                 return 1 + dfs(r-1, c) + dfs(r+1, c) + dfs(r, c-1) + dfs(r, c+1)
            return 0
        for i in range(lr):
            for j in range(lc):
                ans = max(ans, dfs(i,j))
        return ans

原文地址:https://www.cnblogs.com/Johnsonxiong/p/9440473.html

时间: 2024-10-10 13:48:15

[LeetCode] 695. Max Area of Island_Easy tag: DFS/BFS的相关文章

(DFS 图的遍历) leetcode 695. 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) 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

LeetCode 695. 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) 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

200. Number of Islands + 695. 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 surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

【easy】695. 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) 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 isl

695. Max Area of [email&#160;protected]

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

695. Max Area of Island

Problem 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

[LeetCode] 437. Path Sum III_ Easy tag: DFS

You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to

[LeetCode] 257. Binary Tree Paths_ Easy tag: DFS

Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example: Input: 1 / 2 3 5 Output: ["1->2->5", "1->3"] Explanation: All root-to-leaf paths are: 1->2->5, 1->3 思路为DFS, 只是appen

Leetcode题目104.二叉树的最大深度(DFS+BFS简单)

题目描述: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 返回它的最大深度 3 . 思路分析:递归(二叉树最大深度,等于左右子树的最大深度+1) 代码实现: 一.深度优先比遍历(DFS) /** * Definition for a binary tree node. * public class TreeNo