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.
1public class Solution {
2 public int[][] FloodFill(int[][] image, int sr, int sc, int newColor) {
3 int originalColor = image[sr][sc];
4 if(originalColor != newColor) {
5 DFS(image, sr, sc, originalColor, newColor);
6 }
7 return image;
8 }
9
10 private void DFS(int[][] image, int r, int c, int originalColor, int newColor) {
11 if (r < 0 || r >= image.Length || c < 0 || c >= image[0].Length || image[r][c] != originalColor) return;
12 image[r][c] = newColor;
13 DFS(image, r + 1, c, originalColor, newColor);
14 DFS(image, r - 1, c, originalColor, newColor);
15 DFS(image, r, c + 1, originalColor, newColor);
16 DFS(image, r, c - 1, originalColor, newColor);
17 }
18}
The C# approach uses recursion through the DFS
helper method to fill the image, consistent with the approaches in other languages 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.
1#include <vector>
2#include <queue>
3using namespace std;
4
5vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
6 int originalColor = image[sr][sc];
7 if (originalColor == newColor) return image;
8
9 int rows = image.size();
10 int cols = image[0].size();
11 queue<pair<int, int>> q;
12 q.push({sr, sc});
13
14 vector<pair<int, int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
15
16 while (!q.empty()) {
17 auto [r, c] = q.front(); q.pop();
18 if (image[r][c] == originalColor) {
19 image[r][c] = newColor;
20 for (auto [dr, dc] : directions) {
21 int rr = r + dr, cc = c + dc;
22 if (rr >= 0 && rr < rows && cc >= 0 && cc < cols && image[rr][cc] == originalColor) {
23 q.push({rr, cc});
24 }
25 }
26 }
27 }
28
29 return image;
30}
This C++ solution uses the STL queue
, and operations mimic the BFS pattern, checking neighboring pixels and adding them to the queue when they match the original color.