Flood Fill
Repaint the region of same-colored pixels around a starting pixel, spreading with DFS.
The problem
You get an image as a grid of integers image, a starting pixel (sr, sc) and a new color. Repaint the starting pixel and every pixel connected to it through up, down, left or right steps over pixels of the same original color. Return the image after the fill.
Input image = [[3, 3, 0], [0, 3, 3], [3, 0, 3]], sr = 0, sc = 0, color = 7
Output [[7, 7, 0], [0, 7, 7], [3, 0, 7]]
The 3s connected to the top-left pixel become 7. The 3 in the bottom-left corner touches that region only at a corner, so it keeps its color.
Input image = [[5, 5], [5, 1]], sr = 1, sc = 1, color = 1
Output [[5, 5], [5, 1]]
The starting pixel already has color 1, so the image stays the same.
How to spot the pattern
- Starting from one cell, you must reach every connected cell that shares a property.
- The grid is a graph: each pixel is a node joined to its four neighbors.
The idea
The pixels to repaint are exactly the ones you can reach from the start by stepping between neighbors that hold the original color. A DFS that paints each pixel as it enters it does the job, and the new color doubles as the visited mark, because a painted pixel no longer holds the original color. The one trap is a new color equal to the original: nothing needs to change then, and without a check the search would keep entering the same pixels forever.
Brute force: Scan the whole image again and again, repainting any pixel of the original color that touches a repainted one, until nothing changes, which can take O((m * n)^2) time on a winding region.
Step by step
- 1Read the original color
oldat (sr,sc), and return the image unchanged if it already equalscolor. - 2Write
fill(r, c), which stops at once if (r, c) is outside the image or does not holdold. - 3Otherwise paint (r, c) with
color, which also marks it as visited. - 4Call
fillon the four neighbors above, below, left and right. - 5Call
fill(sr, sc)and return the image.
See the pattern move
The graph bfs and dfs pattern on a small example. Step through it, or press play.
Code
class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
old = image[sr][sc]
if old == color:
return image # nothing to change, and the fill would never stop
rows, cols = len(image), len(image[0])
def fill(r: int, c: int) -> None:
if r < 0 or r >= rows or c < 0 or c >= cols or image[r][c] != old:
return
image[r][c] = color # painting the pixel also marks it as visited
fill(r + 1, c)
fill(r - 1, c)
fill(r, c + 1)
fill(r, c - 1)
fill(sr, sc)
return imageComplexity
Time
O(m * n)
Space
O(m * n)
Each pixel is painted at most once and checks four neighbors; when the region snakes through the whole image, the recursion can go m * n calls deep.
Watch out for
- Skipping the check for
old == color, so painted pixels still look unvisited and the recursion never ends. - Treating diagonal pixels as connected; only up, down, left and right count.
- Comparing neighbors with the new color instead of the original one.
Follow-up questions
Can you do it without recursion?
Use a queue (BFS) or your own stack: push the start, and each time you take a pixel out, paint it and push its neighbors that still hold the original color. That avoids deep recursion on big images.
What if diagonal neighbors also count as connected?
Check all 8 surrounding pixels instead of 4; everything else stays the same.
In Hinglish
Start pixel ka purana color yaad rakho, phir DFS se har padosi jo usi color ka hai use naya color do. Naya color hi visited ka nishaan hai, bas pehle check kar lo ki dono color same toh nahi.