-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBomb_361.java
More file actions
33 lines (31 loc) · 1.14 KB
/
Bomb_361.java
File metadata and controls
33 lines (31 loc) · 1.14 KB
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
class Bomb_361 {
// Most voted solution
public int maxKilledEnemies(char[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) return 0;
int R = grid.length;
int C = grid[0].length;
int[] colhits = new int[C];
int res = 0;
for (int i = 0; i < R; i++) {
int rowhits = 0;
for (int j = 0; j < C; j++) {
if (grid[i][j] == 'W') continue;
if (j == 0 || grid[i][j - 1] == 'W') {
rowhits = 0;
for (int k = j; k < C && grid[i][k] != 'W'; k++) {
rowhits += (grid[i][k] == 'E' ? 1 : 0);
}
}
if (i == 0 || grid[i - 1][j] == 'W') {
colhits[j] = 0;
for (int k = i; k < R && grid[k][j] != 'W'; k++) {
colhits[j] += (grid[k][j] == 'E' ? 1 : 0);
}
}
if (grid[i][j] == '0')
res = Math.max(res, rowhits + colhits[j]);
}
}
return res;
}
}