Skip to content

Commit fa5319d

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 18 MB (49.76%)
1 parent 20e37b4 commit fa5319d

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<p>You are given an image represented by an <code>m x n</code> grid of integers <code>image</code>, where <code>image[i][j]</code> represents the pixel value of the image. You are also given three integers <code>sr</code>, <code>sc</code>, and <code>color</code>. Your task is to perform a <strong>flood fill</strong> on the image starting from the pixel <code>image[sr][sc]</code>.</p>
2+
3+
<p>To perform a <strong>flood fill</strong>:</p>
4+
5+
<ol>
6+
<li>Begin with the starting pixel and change its color to <code>color</code>.</li>
7+
<li>Perform the same process for each pixel that is <strong>directly adjacent</strong> (pixels that share a side with the original pixel, either horizontally or vertically) and shares the <strong>same color</strong> as the starting pixel.</li>
8+
<li>Keep <strong>repeating</strong> this process by checking neighboring pixels of the <em>updated</em> pixels&nbsp;and modifying their color if it matches the original color of the starting pixel.</li>
9+
<li>The process <strong>stops</strong> when there are <strong>no more</strong> adjacent pixels of the original color to update.</li>
10+
</ol>
11+
12+
<p>Return the <strong>modified</strong> image after performing the flood fill.</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
17+
<div class="example-block">
18+
<p><strong>Input:</strong> <span class="example-io">image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2</span></p>
19+
20+
<p><strong>Output:</strong> <span class="example-io">[[2,2,2],[2,2,0],[2,0,1]]</span></p>
21+
22+
<p><strong>Explanation:</strong></p>
23+
24+
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/06/01/flood1-grid.jpg" style="width: 613px; height: 253px;" /></p>
25+
26+
<p>From the center of the image with position <code>(sr, sc) = (1, 1)</code> (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color.</p>
27+
28+
<p>Note the bottom corner is <strong>not</strong> colored 2, because it is not horizontally or vertically connected to the starting pixel.</p>
29+
</div>
30+
31+
<p><strong class="example">Example 2:</strong></p>
32+
33+
<div class="example-block">
34+
<p><strong>Input:</strong> <span class="example-io">image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0</span></p>
35+
36+
<p><strong>Output:</strong> <span class="example-io">[[0,0,0],[0,0,0]]</span></p>
37+
38+
<p><strong>Explanation:</strong></p>
39+
40+
<p>The starting pixel is already colored with 0, which is the same as the target color. Therefore, no changes are made to the image.</p>
41+
</div>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>m == image.length</code></li>
48+
<li><code>n == image[i].length</code></li>
49+
<li><code>1 &lt;= m, n &lt;= 50</code></li>
50+
<li><code>0 &lt;= image[i][j], color &lt; 2<sup>16</sup></code></li>
51+
<li><code>0 &lt;= sr &lt; m</code></li>
52+
<li><code>0 &lt;= sc &lt; n</code></li>
53+
</ul>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
3+
directions = (-1, 0, 1, 0, -1)
4+
original_colour = image[sr][sc]
5+
6+
def dfs(i: int, j: int):
7+
image[i][j] = color
8+
for val1, val2 in pairwise(directions):
9+
x, y = i + val1, j + val2
10+
if 0 <= x < len(image) and 0 <= y < len(image[0]) and image[x][y] == original_colour:
11+
dfs(x, y)
12+
13+
if original_colour != color:
14+
dfs(sr, sc)
15+
return image

0 commit comments

Comments
 (0)