KatInterview

361. Bomb Enemy

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

Method: Dynamic Programming with Space Optimization

This approach optimizes the calculation of kill counts by avoiding redundant computations. For any empty cell, the number of enemies killed in its row is constant for all cells within the same wall-to-wall segment. The same logic applies to columns.

Solution

Bomb Enemy in Python

class Solution:
    def maxKilledEnemies(self, grid: List[List[str]]) -> int:
        if not grid or not grid[0]:
            return 0
        
        m, n = len(grid), len(grid[0])
        max_kills = 0
        row_hits = 0  # Caches the number of enemies in the current row segment
        col_hits = [0] * n  # Caches the number of enemies in the current column segment for each column

        for i in range(m):
            for j in range(n):
                # If we are at the beginning of a row or after a wall, recalculate row_hits
                if j == 0 or grid[i][j - 1] == 'W':
                    row_hits = 0
                    # Scan right to count enemies until the next wall
                    for k in range(j, n):
                        if grid[i][k] == 'W':
                            break
                        if grid[i][k] == 'E':
                            row_hits += 1

                # If we are at the beginning of a column or after a wall, recalculate col_hits[j]
                if i == 0 or grid[i - 1][j] == 'W':
                    col_hits[j] = 0
                    # Scan down to count enemies until the next wall
                    for k in range(i, m):
                        if grid[k][j] == 'W':
                            break
                        if grid[k][j] == 'E':
                            col_hits[j] += 1
                
                # If the current cell is an empty spot, calculate potential kills
                if grid[i][j] == '0':
                    max_kills = max(max_kills, row_hits + col_hits[j])
        
        return max_kills

Bomb Enemy in Java

class Solution {
    public int maxKilledEnemies(char[][] grid) {
        if (grid == null || grid.length == 0 || grid[0].length == 0) {
            return 0;
        }
        int m = grid.length;
        int n = grid[0].length;
        
        int maxKills = 0;
        int rowHits = 0; // Caches the number of enemies in the current row segment
        int[] colHits = new int[n]; // Caches the number of enemies in the current column segment for each column

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                // If we are at the beginning of a row or after a wall, recalculate rowHits
                if (j == 0 || grid[i][j - 1] == 'W') {
                    rowHits = 0;
                    // Scan right to count enemies until the next wall
                    for (int k = j; k < n && grid[i][k] != 'W'; k++) {
                        if (grid[i][k] == 'E') {
                            rowHits++;
                        }
                    }
                }

                // If we are at the beginning of a column or after a wall, recalculate colHits[j]
                if (i == 0 || grid[i - 1][j] == 'W') {
                    colHits[j] = 0;
                    // Scan down to count enemies until the next wall
                    for (int k = i; k < m && grid[k][j] != 'W'; k++) {
                        if (grid[k][j] == 'E') {
                            colHits[j]++;
                        }
                    }
                }

                // If the current cell is an empty spot, calculate potential kills
                if (grid[i][j] == '0') {
                    maxKills = Math.max(maxKills, rowHits + colHits[j]);
                }
            }
        }
        
        return maxKills;
    }
}

Bomb Enemy in C++

class Solution {
public:
    int maxKilledEnemies(vector<vector<char>>& grid) {
        int m = grid.size();
        if (m == 0) return 0;
        int n = grid[0].size();
        if (n == 0) return 0;

        int max_kills = 0;
        int row_hits = 0;          // Caches the number of enemies in the current row segment
        vector<int> col_hits(n, 0); // Caches the number of enemies in the current column segment for each column

        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                // If we are at the beginning of a row or after a wall, recalculate row_hits
                if (j == 0 || grid[i][j - 1] == 'W') {
                    row_hits = 0;
                    // Scan right to count enemies until the next wall
                    for (int k = j; k < n && grid[i][k] != 'W'; ++k) {
                        if (grid[i][k] == 'E') {
                            row_hits++;
                        }
                    }
                }

                // If we are at the beginning of a column or after a wall, recalculate col_hits[j]
                if (i == 0 || grid[i - 1][j] == 'W') {
                    col_hits[j] = 0;
                    // Scan down to count enemies until the next wall
                    for (int k = i; k < m && grid[k][j] != 'W'; ++k) {
                        if (grid[k][j] == 'E') {
                            col_hits[j]++;
                        }
                    }
                }

                // If the current cell is an empty spot, calculate potential kills
                if (grid[i][j] == '0') {
                    max_kills = max(max_kills, row_hits + col_hits[j]);
                }
            }
        }

        return max_kills;
    }
};

Bomb Enemy in TypeScript

function maxKilledEnemies(grid: string[][]): number {
    if (!grid || grid.length === 0 || grid[0].length === 0) {
        return 0;
    }
    const m = grid.length;
    const n = grid[0].length;

    let maxKills = 0;
    let rowHits = 0; // Caches the number of enemies in the current row segment
    const colHits: number[] = new Array(n).fill(0); // Caches enemies for current column segments

    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            // If we are at the beginning of a row or after a wall, recalculate rowHits
            if (j === 0 || grid[i][j - 1] === 'W') {
                rowHits = 0;
                // Scan right to count enemies until the next wall
                for (let k = j; k < n && grid[i][k] !== 'W'; k++) {
                    if (grid[i][k] === 'E') {
                        rowHits++;
                    }
                }
            }

            // If we are at the beginning of a column or after a wall, recalculate colHits[j]
            if (i === 0 || grid[i - 1][j] === 'W') {
                colHits[j] = 0;
                // Scan down to count enemies until the next wall
                for (let k = i; k < m && grid[k][j] !== 'W'; k++) {
                    if (grid[k][j] === 'E') {
                        colHits[j]++;
                    }
                }
            }

            // If the current cell is an empty spot, calculate potential kills
            if (grid[i][j] === '0') {
                maxKills = Math.max(maxKills, rowHits + colHits[j]);
            }
        }
    }
    
    return maxKills;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.