Golang solution


  • 0
    R
    func maxKilledEnemies(grid [][]byte) int {
               if grid == nil || len(grid) == 0 ||  len(grid[0]) == 0 { return 0 }
        max := 0;
        row := 0;
        col := make([]int, len(grid[0]))
        for k, _ := range col {
            col[k] = 0
        }
        
        for i := 0; i<len(grid); i++ {
            for  j:= 0; j<len(grid[0]);j++ {
                if grid[i][j] == 'W' {continue }
                if j == 0 || grid[i][j-1] == 'W' {
                     row = killedEnemiesRow(grid, i, j)
                }
                if i == 0 || grid[i-1][j] == 'W' {
                     col[j] = killedEnemiesCol(grid,i,j)
                }
                if grid[i][j] == '0' {
                    if row + col[j] > max {
                        max = row + col[j]
                    } 
                }
            }
    
        }
        
        return max    
    }
    
    func killedEnemiesRow(grid [][]byte, i int, j int) int{
        num := 0;
        for j <= len(grid[0])-1 && grid[i][j] != 'W' {
            if(grid[i][j] == 'E') {num++}
            j++
        }
        return num
    }
    
    func killedEnemiesCol( grid [][]byte, i int , j int) int{
        num := 0;
        for i <= len(grid) -1 && grid[i][j] != 'W' {
            if(grid[i][j] == 'E') {num++ }
            i++
        }
        return num
    }
    

Log in to reply
 

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