File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int maxKilledEnemies (char [][] grid ) {
3
+ if (grid .length == 0 ) {
4
+ return 0 ;
5
+ }
6
+ int rows = grid .length ;
7
+ int cols = grid [0 ].length ;
8
+ int maxCount = 0 ;
9
+ int rowHits = 0 ;
10
+ int [] colHits = new int [cols ];
11
+ for (int i = 0 ; i < rows ; i ++) {
12
+ for (int j = 0 ; j < cols ; j ++) {
13
+ if (j == 0 || grid [i ][j - 1 ] == 'W' ) {
14
+ rowHits = 0 ;
15
+ for (int k = j ; k < cols ; k ++) {
16
+ if (grid [i ][k ] == 'W' ) {
17
+ break ;
18
+ }
19
+ if (grid [i ][k ] == 'E' ) {
20
+ rowHits ++;
21
+ }
22
+ }
23
+ }
24
+ if (i == 0 || grid [i - 1 ][j ] == 'W' ) {
25
+ colHits [j ] = 0 ;
26
+ for (int k = i ; k < rows ; k ++) {
27
+ if (grid [k ][j ] == 'W' ) {
28
+ break ;
29
+ }
30
+ if (grid [k ][j ] == 'E' ) {
31
+ colHits [j ]++;
32
+ }
33
+ }
34
+ }
35
+ if (grid [i ][j ] == '0' ) {
36
+ maxCount = Math .max (maxCount , rowHits + colHits [j ]);
37
+ }
38
+ }
39
+ }
40
+ return maxCount ;
41
+ }
42
+ }
You can’t perform that action at this time.
0 commit comments