361.Bomb-Enemy

361. Bomb Enemy

题目地址

https://leetcode.com/problems/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: You can only put the bomb at an empty cell.

Example:
Input: [["0","E","0","0"],["E","0","W","E"],["0","E","0","0"]]
Output: 3 
Explanation: For the given grid,

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

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

代码

Approach #1 Two loop

public class Solution {
    public int maxKilledEnemies(char[][] grid) {
        if (grid== null || grid.length == 0 || grid[0].length == 0) return 0; 
        int rows = grid.length; 
        int cols = grid[0].length; 
        int max = 0; 
        int[][] dp = new int[rows][cols];
        //travel each column twice: from left and from right
        for (int i = 0; i < rows; i++){
            int cnt = 0; 
            for (int k = 0; k < cols; k++) {
                if (grid[i][k] == '0') {
                    dp[i][k] = cnt; 
                } else if (grid[i][k] == 'E') {
                    cnt++;
                } else {
                    cnt = 0; 
                }
            }
            cnt = 0; 
            for (int k = cols - 1; k >= 0; k--) {
                if (grid[i][k] == '0'){
                    dp[i][k] += cnt; 
                } else if (grid[i][k] == 'E') {
                    cnt++;
                } else {
                    cnt = 0;
                }
            }
        }
        //travel each row twice: from top and from bottom
        for (int i = 0; i < cols; i++) {
            int cnt = 0; 
            for (int k = 0; k < rows; k++) {
                if (grid[k][i] == '0') {
                    dp[k][i] += cnt; 
                } else if (grid[k][i] == 'E') {
                    cnt++;
                } else {
                    cnt = 0; 
                }
            }
            cnt = 0; 
            for (int k = rows - 1; k >= 0; k--) {
                if (grid[k][i] == '0'){
                    dp[k][i] += cnt; 
                    max = Math.max(max, dp[k][i]); 
                } else if (grid[k][i] == 'E') {
                    cnt++;
                } else {
                    cnt = 0;
                }
            }
        }
        return max; 
    }
}

Appraoch #2 DP - 1

class Solution {
  public int maxKilledEnemies(char[][] grid) {
    if (grid.length == 0)
        return 0;

    int rows = grid.length;
    int cols = grid[0].length;

    int maxCount = 0, rowHits = 0;
    int[] colHits = new int[cols];

    for (int row = 0; row < rows; ++row) {
      for (int col = 0; col < cols; ++col) {
        // reset the hits on the row, if necessary.
        if (col == 0 || grid[row][col - 1] == 'W') {
            rowHits = 0;
            for (int k = col; k < cols; ++k) {
                if (grid[row][k] == 'W')
                    // stop the scan when we hit the wall.
                    break;
                else if (grid[row][k] == 'E')
                    rowHits += 1;
            }
        }

        // reset the hits on the column, if necessary.
        if (row == 0 || grid[row - 1][col] == 'W') {
            colHits[col] = 0;
            for (int k = row; k < rows; ++k) {
                if (grid[k][col] == 'W')
                    break;
                else if (grid[k][col] == 'E')
                    colHits[col] += 1;
            }
        }

        // run the calculation for the empty cell.
        if (grid[row][col] == '0') {
            maxCount = Math.max(maxCount, rowHits + colHits[col]);
        }
      }
    }

    return maxCount;
  }
}

Approach #2 DP - 2

public int maxKilledEnemies(char[][] grid) {
    int rowNum = grid.length;
    if (rowNum == 0) return 0;
    int colNum = grid[0].length;

    int[][] fromBottom = new int[rowNum][colNum];
    int[][] fromRight = new int[rowNum][colNum];

    for (int i = rowNum - 1; i >= 0; i--) {
        for (int j = colNum - 1; j >= 0; j--) {
            int enemy = grid[i][j] == 'E' ? 1 : 0;
            if (grid[i][j] != 'W') {
                fromBottom[i][j] = (i == rowNum - 1) ? enemy : fromBottom[i+1][j] + enemy;
                fromRight[i][j] = (j == colNum - 1) ? enemy : fromRight[i][j+1] + enemy;
            }
            else {
                fromBottom[i][j] = 0;
                fromRight[i][j] = 0;
            }
        }
    }

    int[] fromTop = new int[colNum];
    int[] fromLeft = new int[rowNum];
    int max = 0;

    for (int i = 0; i < rowNum; i++) {
        for (int j = 0; j < colNum; j++) {
            if (grid[i][j] != '0') {
                fromTop[j] = grid[i][j] == 'W' ? 0 : fromTop[j] + 1;
                fromLeft[i] = grid[i][j] == 'W' ? 0 : fromLeft[i] + 1;
            }
            else {
                int num = fromTop[j] + fromLeft[i] + fromBottom[i][j] + fromRight[i][j];
                max = Math.max(num, max);
            }
        }
    }
    return max;
}

Last updated