Given an m x n grid grid where 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.
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 bomb until it hits the wall since the wall 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","E","W"],["E","0","E"],["W","E","W"]] Output: 0
Example 3:
Input: grid = [["0","E","0"],["0","0","0"],["0","E","0"]] Output: 2
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 500grid[i][j] is either 'W', 'E', or '0'.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force way to solve this problem involves checking every possible spot in the grid where an enemy could be bombed. For each of those spots, we count how many enemies would be hit if a bomb was placed there.
Here's how the algorithm would work step-by-step:
def bomb_enemy_brute_force(grid):
if not grid:
return 0
rows = len(grid)
cols = len(grid[0])
max_enemies_killed = 0
for row in range(rows):
for col in range(cols):
if grid[row][col] == '0':
# Check only empty cells
enemies_killed = count_enemies_killed(grid, row, col)
max_enemies_killed = max(max_enemies_killed, enemies_killed)
return max_enemies_killed
def count_enemies_killed(grid, bomb_row, bomb_col):
rows = len(grid)
cols = len(grid[0])
total_enemies = 0
# Check enemies to the left
for column_to_left in range(bomb_col - 1, -1, -1):
if grid[bomb_row][column_to_left] == 'W':
break
if grid[bomb_row][column_to_left] == 'E':
total_enemies += 1
# Check enemies to the right
for column_to_right in range(bomb_col + 1, cols):
if grid[bomb_row][column_to_right] == 'W':
break
if grid[bomb_row][column_to_right] == 'E':
total_enemies += 1
# Check enemies upwards
for row_above in range(bomb_row - 1, -1, -1):
if grid[row_above][bomb_col] == 'W':
break
if grid[row_above][bomb_col] == 'E':
total_enemies += 1
# Check enemies downwards
for row_below in range(bomb_row + 1, rows):
if grid[row_below][bomb_col] == 'W':
break
if grid[row_below][bomb_col] == 'E':
total_enemies += 1
return total_enemiesThe key to efficiently solving this problem is to pre-calculate how many enemies each row and column can kill independently. Then, for each empty spot, we combine these pre-calculated values to find the maximum number of enemies we can bomb from that location.
Here's how the algorithm would work step-by-step:
def bomb_enemy(grid):
if not grid or not grid[0]:
return 0
rows = len(grid)
cols = len(grid[0])
row_kills_left_to_right = [[0] * cols for _ in range(rows)]
row_kills_right_to_left = [[0] * cols for _ in range(rows)]
col_kills_top_to_bottom = [[0] * cols for _ in range(rows)]
col_kills_bottom_to_top = [[0] * cols for _ in range(rows)]
for i in range(rows):
kill_count = 0
for j in range(cols):
if grid[i][j] == 'E':
kill_count += 1
elif grid[i][j] == 'W':
kill_count = 0
row_kills_left_to_right[i][j] = kill_count
kill_count = 0
for j in range(cols - 1, -1, -1):
if grid[i][j] == 'E':
kill_count += 1
elif grid[i][j] == 'W':
kill_count = 0
row_kills_right_to_left[i][j] = kill_count
for j in range(cols):
kill_count = 0
for i in range(rows):
if grid[i][j] == 'E':
kill_count += 1
elif grid[i][j] == 'W':
kill_count = 0
col_kills_top_to_bottom[i][j] = kill_count
kill_count = 0
for i in range(rows - 1, -1, -1):
if grid[i][j] == 'E':
kill_count += 1
elif grid[i][j] == 'W':
kill_count = 0
col_kills_bottom_to_top[i][j] = kill_count
max_kills = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == '0':
# Summing all four directions to get total kills for this cell
total_kills = row_kills_left_to_right[i][j] + \
row_kills_right_to_left[i][j] + \
col_kills_top_to_bottom[i][j] + \
col_kills_bottom_to_top[i][j]
max_kills = max(max_kills, total_kills)
return max_kills| Case | How to Handle |
|---|---|
| Null or empty grid | Return 0 immediately as there are no bombs or enemies to consider. |
| Grid with only one row or one column | The solution should still correctly calculate the maximum enemies killed along that single row or column. |
| Grid filled entirely with walls ('W') | Return 0, as no bombs can be placed. |
| Grid filled entirely with enemies ('E') | If there is any open space calculate bomb placement for any open cell. |
| Grid filled entirely with empty cells ('0') | Return 0, as placing bombs will not kill any enemies. |
| Large grid (scaling performance) | Optimize the algorithm to avoid redundant calculations by pre-computing row and column enemy counts. |
| Grid with no valid bomb placement positions ('0') | If there are no '0' cells, return 0, as no bombs can be placed. |
| Integer overflow potential when counting enemies in very large rows/columns | Use a data type with larger capacity (e.g., long) to store enemy counts. |