python easy understanding solution


  • 0
    J
    class Solution:
        def maxAreaOfIsland(self, grid):
            """
            :type grid: List[List[int]]
            :rtype: int
            """
            row,col=len(grid),len(grid[0])
            self.notv=[[True for _ in range(col)] for _ in range(row)] #check whether visited, later I saw someone just edit grid also works
            maxarea=0
            def helper(grid,i,j,row,col):
                if self.notv[i][j] and grid[i][j]:
                    self.num=self.num+1
                    self.notv[i][j]=False
                    if i>0:
                        helper(grid,i-1,j,row,col)
                    if i<row-1:
                        helper(grid,i+1,j,row,col)
                    if j>0:
                        helper(grid,i,j-1,row,col)
                    if j<col-1:
                        helper(grid,i,j+1,row,col)
            for i in range(row):
                for j in range(col):
                    self.num=0 # area starting from current point
                    helper(grid,i,j,row,col)
                    maxarea=max(self.num,maxarea)
            return maxarea
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.