This approach uses recursion to explore all connected pixels that need to be updated. Starting from the initial pixel, you recursively attempt to update all four possible directions (up, down, left, right) whenever the neighboring pixel has the same original color.
The recursion ensures that all connected and valid pixels are eventually updated. Make sure to handle the base case where the function stops if the pixel is out of bounds or if it doesn't need updating (either because it's already updated or has a different color).
Time Complexity: O(m * n) because in the worst case, all of the pixels will be connected and thus changed.
Space Complexity: O(m * n) due to the recursion call stack.
1function floodFill(image, sr, sc, newColor) {
2 const originalColor = image[sr][sc];
3 if (originalColor !== newColor) {
4 dfs(image, sr, sc, originalColor, newColor);
5 }
6 return image;
7
8 function dfs(image, r, c, originalColor, newColor) {
9 if (r < 0 || r >= image.length || c < 0 || c >= image[0].length || image[r][c] !== originalColor) return;
10 image[r][c] = newColor;
11 dfs(image, r + 1, c, originalColor, newColor);
12 dfs(image, r - 1, c, originalColor, newColor);
13 dfs(image, r, c + 1, originalColor, newColor);
14 dfs(image, r, c - 1, originalColor, newColor);
15 }
16}
This JavaScript implementation defines an inner function dfs
which recursively updates the image similarly to the other language implementations described.
This approach employs an iterative breadth-first search (BFS) using a queue. Starting from the initial pixel, we enqueue it and begin the BFS process, updating connected pixels of the same original color to the new color.
For each pixel, check its adjacent pixels and enqueue them if they are valid and of the original color. This continues until the queue is empty, ensuring all pixels are updated appropriately.
Time Complexity: O(m * n), where m and n are the dimensions of the image (same as DFS).
Space Complexity: O(m * n), as we may store all pixels in the queue in the worst-case scenario.
1using System.Collections.Generic;
2
3public class Solution {
4 public int[][] FloodFill(int[][] image, int sr, int sc, int newColor) {
5 int originalColor = image[sr][sc];
6 if (originalColor == newColor) return image;
7
8 int rows = image.Length;
9 int cols = image[0].Length;
10 Queue<(int, int)> queue = new Queue<(int, int)>();
11 queue.Enqueue((sr, sc));
12
13 int[][] directions = new int[][] { new int[] {1, 0}, new int[] {-1, 0}, new int[] {0, 1}, new int[] {0, -1} };
14
15 while (queue.Count > 0) {
16 var (r, c) = queue.Dequeue();
17 if (image[r][c] == originalColor) {
18 image[r][c] = newColor;
19 foreach (var d in directions) {
20 int rr = r + d[0], cc = c + d[1];
21 if (rr >= 0 && rr < rows && cc >= 0 && cc < cols && image[rr][cc] == originalColor) {
22 queue.Enqueue((rr, cc));
23 }
24 }
25 }
26 }
27
28 return image;
29 }
30}
The C# implementation employs a Queue
for the BFS logic, exploring and updating pixels iteratively. It checks each adjacent pixel and enqueues eligible pixels until all are processed.