-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBomb Enemy.java
34 lines (34 loc) · 1.2 KB
/
Bomb Enemy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Solution {
/*
* @param grid: Given a 2D grid, each cell is either 'W', 'E' or '0'
* @return: an integer, the maximum enemies you can kill using one bomb
*/
public int maxKilledEnemies(char[][] grid) {
// write your code here
if (grid == null || grid.length == 0) {
return 0;
}
int m = grid.length, n = grid[0].length, res = 0, rowCnt = 0;
int[] colCnt = 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') {
rowCnt = 0;
for (int k = j; k < n && grid[i][k] != 'W'; ++k) {
rowCnt += grid[i][k] == 'E' ? 1 : 0;
}
}
if (i == 0 || grid[i - 1][j] == 'W') {
colCnt[j] = 0;
for (int k = i; k < m && grid[k][j] != 'W'; ++k) {
colCnt[j] += grid[k][j] == 'E' ? 1 : 0;
}
}
if (grid[i][j] == '0') {
res = Math.max(res, rowCnt + colCnt[j]);
}
}
}
return res;
}
}