Leetcode Problem 361. Bomb Enemy

361. Bomb Enemy

Leetcode Solutions

Dynamic Programming Approach

  1. Initialize variables to store the maximum number of enemies killed (maxKills), the number of enemies killed in the current row (rowHits), and an array to store the number of enemies killed in each column (colHits).
  2. Iterate over each cell in the grid.
  3. If the current cell is the start of a new row or follows a wall, recalculate rowHits by scanning to the right until another wall or the end of the row.
  4. If the current cell is the start of a new column or follows a wall, recalculate the corresponding colHits value by scanning downwards until another wall or the end of the column.
  5. If the current cell is empty, calculate the total number of enemies that can be killed by adding rowHits and the corresponding colHits value.
  6. Update maxKills if the total number of enemies killed is greater than the current maxKills.
  7. Return maxKills after iterating over all cells.
UML Thumbnail

Brute-force Enumeration Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...