361. Bomb Enemy

Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return the maximum enemies you can kill using one bomb.
The bomb kills all the enemies in the same row and column from the planted point until it hits the wall since the wall is too strong to be destroyed.
Note that you can only put the bomb at an empty cell.

Example:

For the given grid

0 E 0 0
E 0 W E
0 E 0 0

return 3. (Placing a bomb at (1,1) kills 3 enemies)

Solution

(1) Java

class Solution {
    class Cell {
        int up = 0;
        int down = 0;
        int left = 0;
        int right = 0;
        int self = 0;
    }

    public int maxKilledEnemies(char[][] grid) {
        if (grid == null || grid.length == 0 || grid[0].length == 0) {
            return 0;
        }
        int rst = 0;
        int m = grid.length;
        int n = grid[0].length;
        Cell[][] cells = new Cell[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                Cell cell = new Cell();
                cells[i][j] = cell;
                if (grid[i][j] != 'W') {
                    cell.up = (i == 0 ? 0 : cells[i-1][j].up+cells[i-1][j].self);
                    cell.left = (j == 0 ? 0: cells[i][j-1].left+cells[i][j-1].self);
                    cell.self = grid[i][j] == 'E' ? 1 : 0; 
                }

            }
        }
        for (int i = m-1; i >= 0; i--) {
            for (int j = n-1; j >= 0; j--) {
                if (grid[i][j] != 'W') {
                    Cell cell = cells[i][j];
                    cell.right = (j == n-1 ? 0 : cells[i][j+1].right+cells[i][j+1].self);
                    cell.down = (i == m-1 ? 0 : cells[i+1][j].down+cells[i+1][j].self);
                    if (grid[i][j] == '0') {
                        rst = Math.max(rst, cell.up+cell.down+cell.left+cell.right);
                    }
                }
            }
        }
        return rst;
    }
}

(2) Python

class Solution:
    def maxKilledEnemies(self, grid):
        """
        :type grid: List[List[str]]
        :rtype: int
        """
        if not grid:
            return 0
        rst = 0
        m = len(grid)
        n = len(grid[0])
        rowhits = 0
        colhits = [0]*n
        for i in range(m):
            for j in range(n):
                if j == 0 or grid[i][j-1] == "W":
                    rowhits = 0
                    k = j
                    while k < n and grid[i][k] != "W": 
                        if grid[i][k] == "E":
                            rowhits += 1
                        k += 1
                if i == 0 or grid[i-1][j] == "W":
                    colhits[j] = 0
                    k = i
                    while k < m and grid[k][j] != "W":
                        if grid[k][j] == "E":
                            colhits[j] += 1
                        k += 1
                if grid[i][j] == "0":
                    rst = max(rst, colhits[j]+rowhits)
        return rst

(3) Scala



results matching ""

    No results matching ""