-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0361-bomb-enemy.py
More file actions
38 lines (34 loc) · 1.31 KB
/
0361-bomb-enemy.py
File metadata and controls
38 lines (34 loc) · 1.31 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
34
35
36
37
38
# time complexity: O(r*c)
# space complexity: O(c)
from typing import List
class Solution:
def maxKilledEnemies(self, grid: List[List[str]]) -> int:
ROW = len(grid)
COL = len(grid[0])
maxCount = 0
rowHits = 0
colHits = [0 for _ in range(COL)]
for r in range(ROW):
for c in range(COL):
if c == 0 or grid[r][c - 1] == 'W':
rowHits = 0
for i in range(c, COL):
if grid[r][i] == 'W':
break
elif grid[r][i] == 'E':
rowHits += 1
if r == 0 or grid[r - 1][c] == 'W':
colHits[c] = 0
for i in range(r, ROW):
if grid[i][c] == 'W':
break
elif grid[i][c] == 'E':
colHits[c] += 1
if grid[r][c] == '0':
totalHits = rowHits + colHits[c]
maxCount = max(maxCount, totalHits)
return maxCount
grid = [["0", "E", "0", "0"], ["E", "0", "W", "E"], ["0", "E", "0", "0"]]
print(Solution().maxKilledEnemies(grid))
grid = [["W", "W", "W"], ["0", "0", "0"], ["E", "E", "E"]]
print(Solution().maxKilledEnemies(grid))