Skip to content

Commit 776b39b

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 18 MB (11.72%)
1 parent 1ffd5f4 commit 776b39b

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed

0733-flood-fill/README.md

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,44 @@
1-
<p>An image is represented by an <code>m x n</code> integer grid <code>image</code> where <code>image[i][j]</code> represents the pixel value of the image.</p>
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>
22

3-
<p>You are also given three integers <code>sr</code>, <code>sc</code>, and <code>color</code>. You should perform a <strong>flood fill</strong> on the image starting from the pixel <code>image[sr][sc]</code>.</p>
3+
<p>To perform a <strong>flood fill</strong>:</p>
44

5-
<p>To perform a <strong>flood fill</strong>, consider the starting pixel, plus any pixels connected <strong>4-directionally</strong> to the starting pixel of the same color as the starting pixel, plus any pixels connected <strong>4-directionally</strong> to those pixels (also with the same color), and so on. Replace the color of all of the aforementioned pixels with <code>color</code>.</p>
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>
611

7-
<p>Return <em>the modified image after performing the flood fill</em>.</p>
12+
<p>Return the <strong>modified</strong> image after performing the flood fill.</p>
813

914
<p>&nbsp;</p>
1015
<p><strong class="example">Example 1:</strong></p>
11-
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/01/flood1-grid.jpg" style="width: 613px; height: 253px;" />
12-
<pre>
13-
<strong>Input:</strong> image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2
14-
<strong>Output:</strong> [[2,2,2],[2,2,0],[2,0,1]]
15-
<strong>Explanation:</strong> From the center of the image with position (sr, sc) = (1, 1) (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.
16-
Note the bottom corner is not colored 2, because it is not 4-directionally connected to the starting pixel.
17-
</pre>
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>
1830

1931
<p><strong class="example">Example 2:</strong></p>
2032

21-
<pre>
22-
<strong>Input:</strong> image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0
23-
<strong>Output:</strong> [[0,0,0],[0,0,0]]
24-
<strong>Explanation:</strong> The starting pixel is already colored 0, so no changes are made to the image.
25-
</pre>
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>
2642

2743
<p>&nbsp;</p>
2844
<p><strong>Constraints:</strong></p>

0733-flood-fill/solution.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
# Approach 1 - Depth-First Search
1+
# Approach 1: Depth First Search
22

3-
# Time: O(N)
4-
# Space: O(N)
3+
# Time: O(n)
4+
# Space: O(n)
55

66
class Solution:
7-
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
7+
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
88
R, C = len(image), len(image[0])
9-
color = image[sr][sc]
10-
if color == newColor:
9+
curr_color = image[sr][sc]
10+
if curr_color == color:
1111
return image
12-
12+
1313
def dfs(r, c):
14-
if image[r][c] == color:
15-
image[r][c] = newColor
14+
if image[r][c] == curr_color:
15+
image[r][c] = color
16+
1617
if r >= 1:
17-
dfs(r-1, c)
18-
if r+1 < R:
19-
dfs(r+1, c)
18+
dfs(r - 1, c)
19+
if r + 1 < R:
20+
dfs(r + 1, c)
2021
if c >= 1:
21-
dfs(r, c-1)
22-
if c+1 < C:
23-
dfs(r, c+1)
24-
22+
dfs(r, c - 1)
23+
if c + 1 < C:
24+
dfs(r, c + 1)
25+
2526
dfs(sr, sc)
2627
return image
27-
2828

0 commit comments

Comments
 (0)