Welcome to Subscribe On Youtube
361. Bomb Enemy
Description
Given an m x n
matrix grid
where each cell is either a wall 'W'
, an enemy 'E'
or empty '0'
, return the maximum enemies you can kill using one bomb. You can only place the bomb in an empty cell.
The bomb kills all the enemies in the same row and column from the planted point until it hits the wall since it is too strong to be destroyed.
Example 1:
Input: grid = [["0","E","0","0"],["E","0","W","E"],["0","E","0","0"]] Output: 3
Example 2:
Input: grid = [["W","W","W"],["0","0","0"],["E","E","E"]] Output: 1
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 500
grid[i][j]
is either'W'
,'E'
, or'0'
.
Solutions
-
class Solution { public int maxKilledEnemies(char[][] grid) { int m = grid.length; int n = grid[0].length; int[][] g = new int[m][n]; for (int i = 0; i < m; ++i) { int t = 0; for (int j = 0; j < n; ++j) { if (grid[i][j] == 'W') { t = 0; } else if (grid[i][j] == 'E') { ++t; } g[i][j] += t; } t = 0; for (int j = n - 1; j >= 0; --j) { if (grid[i][j] == 'W') { t = 0; } else if (grid[i][j] == 'E') { ++t; } g[i][j] += t; } } for (int j = 0; j < n; ++j) { int t = 0; for (int i = 0; i < m; ++i) { if (grid[i][j] == 'W') { t = 0; } else if (grid[i][j] == 'E') { ++t; } g[i][j] += t; } t = 0; for (int i = m - 1; i >= 0; --i) { if (grid[i][j] == 'W') { t = 0; } else if (grid[i][j] == 'E') { ++t; } g[i][j] += t; } } int ans = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == '0') { ans = Math.max(ans, g[i][j]); } } } return ans; } }
-
class Solution { public: int maxKilledEnemies(vector<vector<char>>& grid) { int m = grid.size(), n = grid[0].size(); vector<vector<int>> g(m, vector<int>(n)); for (int i = 0; i < m; ++i) { int t = 0; for (int j = 0; j < n; ++j) { if (grid[i][j] == 'W') t = 0; else if (grid[i][j] == 'E') ++t; g[i][j] += t; } t = 0; for (int j = n - 1; j >= 0; --j) { if (grid[i][j] == 'W') t = 0; else if (grid[i][j] == 'E') ++t; g[i][j] += t; } } for (int j = 0; j < n; ++j) { int t = 0; for (int i = 0; i < m; ++i) { if (grid[i][j] == 'W') t = 0; else if (grid[i][j] == 'E') ++t; g[i][j] += t; } t = 0; for (int i = m - 1; i >= 0; --i) { if (grid[i][j] == 'W') t = 0; else if (grid[i][j] == 'E') ++t; g[i][j] += t; } } int ans = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == '0') ans = max(ans, g[i][j]); } } return ans; } };
-
class Solution: def maxKilledEnemies(self, grid: List[List[str]]) -> int: m, n = len(grid), len(grid[0]) g = [[0] * n for _ in range(m)] for i in range(m): t = 0 for j in range(n): if grid[i][j] == 'W': t = 0 elif grid[i][j] == 'E': t += 1 g[i][j] += t t = 0 for j in range(n - 1, -1, -1): if grid[i][j] == 'W': t = 0 elif grid[i][j] == 'E': t += 1 g[i][j] += t for j in range(n): t = 0 for i in range(m): if grid[i][j] == 'W': t = 0 elif grid[i][j] == 'E': t += 1 g[i][j] += t t = 0 for i in range(m - 1, -1, -1): if grid[i][j] == 'W': t = 0 elif grid[i][j] == 'E': t += 1 g[i][j] += t return max( [g[i][j] for i in range(m) for j in range(n) if grid[i][j] == '0'], default=0, )
-
func maxKilledEnemies(grid [][]byte) int { m, n := len(grid), len(grid[0]) g := make([][]int, m) for i := range g { g[i] = make([]int, n) } for i := 0; i < m; i++ { t := 0 for j := 0; j < n; j++ { if grid[i][j] == 'W' { t = 0 } else if grid[i][j] == 'E' { t++ } g[i][j] += t } t = 0 for j := n - 1; j >= 0; j-- { if grid[i][j] == 'W' { t = 0 } else if grid[i][j] == 'E' { t++ } g[i][j] += t } } for j := 0; j < n; j++ { t := 0 for i := 0; i < m; i++ { if grid[i][j] == 'W' { t = 0 } else if grid[i][j] == 'E' { t++ } g[i][j] += t } t = 0 for i := m - 1; i >= 0; i-- { if grid[i][j] == 'W' { t = 0 } else if grid[i][j] == 'E' { t++ } g[i][j] += t } } ans := 0 for i, row := range grid { for j, v := range row { if v == '0' && ans < g[i][j] { ans = g[i][j] } } } return ans }