# Easy understanding solution using one matrix C#

• Basically, calculate enemies from left to right, then right to left, do this for each row and column. Finally find max in the matrix.

``````public class Solution {
public int MaxKilledEnemies(char[,] grid) {
int m = grid.GetLength(0), n = grid.GetLength(1);
if(m==0 || n==0){ return 0; }
int[,] kill = new int[m, n];
for(int i=0; i<m; i++){
int enemies = 0;
for(int j=0; j<n; j++){
calculate(grid, kill, i, j, ref enemies);
}
enemies = 0;
for(int j=n-1; j>=0; j--){
calculate(grid, kill, i, j, ref enemies);
}
}
for(int j=0; j<n; j++){
int enemies = 0;
for(int i=0; i<m; i++){
calculate(grid, kill, i, j, ref enemies);
}
enemies = 0;
for(int i=m-1; i>=0; i--){
calculate(grid, kill, i, j, ref enemies);
}
}
return findMax(kill, m, n);
}

private int findMax(int[,] kill, int m, int n){
int max = kill[0, 0];
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
max = Math.Max(max, kill[i, j]);
}
}
return max;
}

private void calculate(char[,] grid, int[,] kill, int i, int j, ref int enemies){
if(grid[i, j]=='E'){
enemies++;
}
else if(grid[i, j]=='W'){
enemies=0;
}
else { kill[i, j] += enemies; }
}
}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.