public class Solution {
public int maxKilledEnemies(char[][] grid) {
if (grid == null || grid.length < 1 || grid[0].length < 1) return 0;
int m = grid.length;
int n = grid[0].length;
int res = 0;
int rowhit = 0;
int[] colhits = new int[n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (j == 0 || grid[i][j - 1] == 'W') {
rowhit = 0;
for (int k = j; k < n && grid[i][k] != 'W'; k++) {
if (grid[i][k] == 'E') rowhit++;
}
}
if (i == 0 || grid[i - 1][j] == 'W') {
colhits[j] = 0;
for (int k = i; k < m && grid[k][j] != 'W'; k++) {
if (grid[k][j] == 'E') colhits[j]++;
}
}
if (grid[i][j] == '0') {
res = Math.max(res, rowhit + colhits[j]);
}
}
}
return res;
}
}
public class Solution {
public int maxKilledEnemies(char[][] grid) {
if (grid.length == 0 || grid[0].length == 0) return 0;
int m = grid.length, n = grid[0].length;
int[] cols = new int[grid[0].length];
int curRow = 0;
for (int i = 0; i < m; i++) {
curRow = 0;
for (int j = 0; j < n; j++) {
if (grid[i][j] == 'E') {
cols[j] ++;
curRow ++;
} else if (grid[i][j] == 'W') {
cols[j] = 0;
curRow = 0;
} else {
grid[i][j] = (char)('X' + cols[j] + curRow);
}
}
}
int res = 0;
Arrays.fill(cols, 0);
for (int i = m - 1; i >= 0; i--) {
curRow = 0;
for (int j = n - 1; j >= 0; j--) {
if (grid[i][j] == 'E') {
cols[j] ++;
curRow ++;
} else if (grid[i][j] == 'W') {
cols[j] = 0;
curRow = 0;
} else {
res = Math.max(res, grid[i][j] - 'X' + cols[j] + curRow);
}
}
}
return res;
}
}