-
Notifications
You must be signed in to change notification settings - Fork 0
Bomb Enemy (atten)
Tim_Gao edited this page Sep 17, 2016
·
1 revision
- idea
public class Solution {
public int maxKilledEnemies(char[][] grid) {
if(grid.length == 0 || grid[0].length == 0){
return 0;
}
int maxNum = 0;
int[] rowHits = new int[grid.length], colHits = new int[grid[0].length];
for (int i=0; i<grid.length; ++i){
for (int j=0; j<grid[0].length; ++j){
if(i==0 || grid[i-1][j] == 'W'){
colHits[j] = 0;
for (int k=i; k<grid.length; ++k){
if(grid[k][j] == 'W'){
break;
} else if(grid[k][j] == 'E'){
++colHits[j];
}
}
}
if (j == 0 || grid[i][j-1] == 'W'){
rowHits[i] = 0;
for (int k=j; k<grid[0].length; ++k){
if(grid[i][k] == 'W'){
break;
} else if (grid[i][k] == 'E'){
++rowHits[i];
}
}
}
if (grid[i][j] == '0'){
maxNum = Math.max(maxNum, colHits[j] + rowHits[i]);
}
}
}
return maxNum;
}
}