Flood Fill
Pattern: Grid graphs
Starting from the given cell, repaint every cell connected to it (up, down, left, right) that has the same original colour. Painting a cell also marks it as done; if the new colour equals the old one, change nothing.
The problem
Given an image (grid of numbers), a start cell (sr, sc) and a colour, change the start cell and every cell connected
to it by same-coloured cells (horizontally or vertically) to the new colour. Return the image.
Think first
- Which cells change?
- How do you avoid painting a cell twice?
- What if the new colour equals the old one?
Hints
Brute force, and why it fails
Repeatedly scan the grid for cells next to painted ones: O((R × C)²).
The key insight
A DFS from the start visits exactly the connected same-coloured cells, each once.
Dry run
| old | 1 |
|---|
image [[1, 1, 1], [1, 1, 0], [1, 0, 1]], start (1, 1), colour 2. The old colour is 1.
Step 1 of 3
The plan
old = image[sr][sc]; if old == color: return
paint(r, c): if inside and image[r][c] == old: set to color; paint the four neighbours
Complexity
- Time
O(R × C)- Space
O(R × C)
Each cell is painted once.
Edge cases
- The new colour equals the old one.
- A 1 × 1 image.
- The whole image one colour.
I've built my logic, show the code
Java
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int color) {
int old = image[sr][sc];
if (old != color) paint(image, sr, sc, old, color); // nothing to do if it is already that colour
return image;
}
private void paint(int[][] g, int r, int c, int old, int color) {
if (r < 0 || c < 0 || r >= g.length || c >= g[0].length || g[r][c] != old) return;
g[r][c] = color; // painting it also marks it as done
paint(g, r + 1, c, old, color); paint(g, r - 1, c, old, color);
paint(g, r, c + 1, old, color); paint(g, r, c - 1, old, color);
}
}C++
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
int old = image[sr][sc];
if (old != color) paint(image, sr, sc, old, color); // nothing to do if it is already that colour
return image;
}
void paint(vector<vector<int>>& g, int r, int c, int old, int color) {
if (r < 0 || c < 0 || r >= static_cast<int>(g.size()) || c >= static_cast<int>(g[0].size()) || g[r][c] != old) return;
g[r][c] = color; // painting it also marks it as done
paint(g, r + 1, c, old, color); paint(g, r - 1, c, old, color);
paint(g, r, c + 1, old, color); paint(g, r, c - 1, old, color);
}
};Python
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 do if it is already that colour
R, C = len(image), len(image[0])
image[sr][sc] = color # painting a cell also marks it as done
st = [(sr, sc)]
while st:
r, c = st.pop()
for x, y in ((r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)):
if 0 <= x < R and 0 <= y < C and image[x][y] == old:
image[x][y] = color
st.append((x, y))
return imageCommon mistakes
- Forgetting the same-colour check, which loops forever.
- Painting diagonal neighbours.