Skip to content

Commit 88c3ff0

Browse files
committed
Sync LeetCode submission Runtime - 71 ms (28.37%), Memory - 21.6 MB (16.62%)
1 parent 0dd77ec commit 88c3ff0

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p>You are given an <code>m x n</code> binary matrix <code>image</code> where <code>0</code> represents a white pixel and <code>1</code> represents a black pixel.</p>
2+
3+
<p>The black pixels are connected (i.e., there is only one black region). Pixels are connected horizontally and vertically.</p>
4+
5+
<p>Given two integers <code>x</code> and <code>y</code> that represents the location of one of the black pixels, return <em>the area of the smallest (axis-aligned) rectangle that encloses all black pixels</em>.</p>
6+
7+
<p>You must write an algorithm with less than <code>O(mn)</code> runtime complexity</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/pixel-grid.jpg" style="width: 333px; height: 253px;" />
12+
<pre>
13+
<strong>Input:</strong> image = [[&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;],[&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;],[&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;]], x = 0, y = 2
14+
<strong>Output:</strong> 6
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> image = [[&quot;1&quot;]], x = 0, y = 0
21+
<strong>Output:</strong> 1
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
<p><strong>Constraints:</strong></p>
26+
27+
<ul>
28+
<li><code>m == image.length</code></li>
29+
<li><code>n == image[i].length</code></li>
30+
<li><code>1 &lt;= m, n &lt;= 100</code></li>
31+
<li><code>image[i][j]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
32+
<li><code>0 &lt;= x &lt; m</code></li>
33+
<li><code>0 &lt;= y &lt; n</code></li>
34+
<li><code>image[x][y] == &#39;1&#39;.</code></li>
35+
<li>The black pixels in the <code>image</code> only form <strong>one component</strong>.</li>
36+
</ul>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Approach 2: DFS
2+
3+
# Time: O(mn)
4+
# Space: O(mn)
5+
6+
class Solution:
7+
def minArea(self, image: List[List[str]], x: int, y: int) -> int:
8+
if not image or not image[0]:
9+
return 0
10+
11+
self.top = self.bottom = x
12+
self.left = self.right = y
13+
14+
def dfs(x, y):
15+
if (x < 0 or y < 0 or
16+
x >= len(image) or y >= len(image[0]) or
17+
image[x][y] == '0'):
18+
return
19+
20+
image[x][y] = '0' # Mark visited black pixel as white
21+
22+
self.top = min(self.top, x)
23+
self.bottom = max(self.bottom, x + 1)
24+
self.left = min(self.left, y)
25+
self.right = max(self.right, y + 1)
26+
27+
dfs(x + 1, y)
28+
dfs(x - 1, y)
29+
dfs(x, y - 1)
30+
dfs(x, y + 1)
31+
32+
dfs(x, y)
33+
return (self.right - self.left) * (self.bottom - self.top)
34+

0 commit comments

Comments
 (0)