Leetcode每日一题2021/01/21

Leetcode每日一题2021/01/21_第1张图片
Leetcode每日一题2021/01/21_第2张图片

class Solution:
    def maxAreaOfIsland(self, grid: List[List[int]]) -> int:

        def DFS(grid, i , j):
            if(i >= len(grid) or i < 0 or j < 0 or j >= len(grid[0])): return 0
            if(grid[i][j] == 0): return 0
            grid[i][j] = 0
            return 1 + DFS(grid, i-1, j) + DFS(grid, i+1, j) + DFS(grid, i, j-1) + DFS(grid, i, j+1)

        row, colunm = len(grid), len(grid[0])
        maxArea = 0
        for i in range(row):
            for j in range(colunm):
                if(grid[i][j] == 1):
                    maxArea = max(maxArea, DFS(grid, i, j))
        return maxArea

Leetcode每日一题2021/01/21_第3张图片

你可能感兴趣的:(Leetcode,dfs,leetcode,算法,python)