Golang solution


  • 0
    M
    func maxAreaOfIsland(grid [][]int) int {
        maxArea := 0
        for y, maxY := 0, len(grid); y < maxY; y++ {
            for x, maxX := 0, len(grid[y]); x < maxX; x++ {
                if grid[y][x] > 0 {
                    if sum := sumIslandArea(grid, y, x); sum > maxArea {
                        maxArea = sum
                    }
                }
            }
        }
        return maxArea
    }
    
    func sumIslandArea(grid [][]int, y int, x int) int {
        sum := 1
        grid[y][x] = 0
    
        maxY, maxX := len(grid), len(grid[y])
        if y > 0 && grid[y-1][x] > 0 {
            sum += sumIslandArea(grid, y-1, x)
        }
        if x < maxX-1 && grid[y][x+1] > 0 {
            sum += sumIslandArea(grid, y, x+1)
        }
        if y < maxY-1 && grid[y+1][x] > 0 {
            sum += sumIslandArea(grid, y+1, x)
        }
        if x > 0 && grid[y][x-1] > 0 {
            sum += sumIslandArea(grid, y, x-1)
        }
        return sum
    }
    

Log in to reply
 

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