Skip to content

Commit fc231ab

Browse files
committed
Sync LeetCode submission Runtime - 114 ms (90.97%), Memory - 45.5 MB (60.45%)
1 parent e581128 commit fc231ab

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

0361-bomb-enemy/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<p>Given an <code>m x n</code> matrix <code>grid</code> where each cell is either a wall <code>&#39;W&#39;</code>, an enemy <code>&#39;E&#39;</code> or empty <code>&#39;0&#39;</code>, return <em>the maximum enemies you can kill using one bomb</em>. You can only place the bomb in an empty cell.</p>
2+
3+
<p>The bomb kills all the enemies in the same row and column from the planted point until it hits the wall since it is too strong to be destroyed.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/27/bomb1-grid.jpg" style="width: 600px; height: 187px;" />
8+
<pre>
9+
<strong>Input:</strong> grid = [[&quot;0&quot;,&quot;E&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;E&quot;,&quot;0&quot;,&quot;W&quot;,&quot;E&quot;],[&quot;0&quot;,&quot;E&quot;,&quot;0&quot;,&quot;0&quot;]]
10+
<strong>Output:</strong> 3
11+
</pre>
12+
13+
<p><strong class="example">Example 2:</strong></p>
14+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/27/bomb2-grid.jpg" style="width: 500px; height: 194px;" />
15+
<pre>
16+
<strong>Input:</strong> grid = [[&quot;W&quot;,&quot;W&quot;,&quot;W&quot;],[&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;E&quot;,&quot;E&quot;,&quot;E&quot;]]
17+
<strong>Output:</strong> 1
18+
</pre>
19+
20+
<p>&nbsp;</p>
21+
<p><strong>Constraints:</strong></p>
22+
23+
<ul>
24+
<li><code>m == grid.length</code></li>
25+
<li><code>n == grid[i].length</code></li>
26+
<li><code>1 &lt;= m, n &lt;= 500</code></li>
27+
<li><code>grid[i][j]</code> is either <code>&#39;W&#39;</code>, <code>&#39;E&#39;</code>, or <code>&#39;0&#39;</code>.</li>
28+
</ul>

0361-bomb-enemy/solution.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution:
2+
def maxKilledEnemies(self, grid: List[List[str]]) -> int:
3+
if len(grid) == 0:
4+
return 0
5+
6+
rows, cols = len(grid), len(grid[0])
7+
8+
max_count = 0
9+
row_hits = 0
10+
col_hits = [0] * cols
11+
12+
for row in range(0, rows):
13+
for col in range(0, cols):
14+
# reset the hits on the row, if necessary.
15+
if col == 0 or grid[row][col - 1] == 'W':
16+
row_hits = 0
17+
for k in range(col, cols):
18+
if grid[row][k] == 'W':
19+
# stop the scan when we hit the wall.
20+
break
21+
elif grid[row][k] == 'E':
22+
row_hits += 1
23+
24+
# reset the hits on the col, if necessary.
25+
if row == 0 or grid[row - 1][col] == 'W':
26+
col_hits[col] = 0
27+
for k in range(row, rows):
28+
if grid[k][col] == 'W':
29+
break
30+
elif grid[k][col] == 'E':
31+
col_hits[col] += 1
32+
33+
# count the hits for each empty cell.
34+
if grid[row][col] == '0':
35+
total_hits = row_hits + col_hits[col]
36+
max_count = max(max_count, total_hits)
37+
38+
return max_count

0 commit comments

Comments
 (0)