361. Bomb Enemy
Bomb Enemy
Solution
public class Solution {
public int maxKilledEnemies(char[][] grid) {
if (grid == null || grid.length == 0) return 0;
int row = grid.length;
int column = grid[0].length;
int[][] numbers = new int[row][column];
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
if (grid[i][j] == 'E') {
int up = i - 1;
while (up >= 0 && grid[up][j] != 'W') {
numbers[up][j] += 1;
up--;
}
up = i + 1;
while (up < row && grid[up][j] != 'W') {
numbers[up][j] += 1;
up++;
}
up = j - 1;
while (up >= 0 && grid[i][up] != 'W') {
numbers[i][up] += 1;
up--;
}
up = j + 1;
while (up < column && grid[i][up] != 'W') {
numbers[i][up] += 1;
up++;
}
}
}
}
int result = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
if (grid[i][j] != 'E') {
result = Math.max(result, numbers[i][j]);
}
}
}
return result;
}
}Last updated