Skip to content

0733. Flood Fill

An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image.

You are also given three integers sr, sc, and newColor. You should perform a flood fill on the image starting from the pixel image[sr][sc].

To perform a flood fill, consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color), and so on. Replace the color of all of the aforementioned pixels with newColor.

Return the modified image after performing the flood fill.

Example 1:

img

Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, newColor = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
Explanation: 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.
Note the bottom corner is not colored 2, because it is not 4-directionally connected to the starting pixel.

Example 2:

Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, newColor = 2
Output: [[2,2,2],[2,2,2]]

Constraints:

  • m == image.length
  • n == image[i].length
  • 1 <= m, n <= 50
  • 0 <= image[i][j], newColor < 216
  • 0 <= sr < m
  • 0 <= sc < n

Analysis

We can use both DFS or BFS to solve this problem, and depending on the requirement, DFS would require to set the call stack space to hold the recursion tree, and BFS requires to create a stack space that is at most equal to the size of grid.

  • Time: O(n^2) or O(w * h)
  • Space: O(n^2) or O(w * h)

Code: DFS

class Solution {
public:
  int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  void dfs(int x, int y, int org, int color, vector<vector<int>> &g) {
    if (x < 0 || x >= g.size() || y < 0 || y >= g[0].size() || g[x][y] != org || g[x][y] == color)
      return;
    g[x][y] = color;
    for (auto d : dir) {
      dfs(x + d[0], y + d[1], org, color, g);
    }
  }
  vector<vector<int>> floodFill(vector<vector<int>> &image, int sr, int sc,
                                int newColor) {
    dfs(sr, sc, image[sr][sc], newColor, image);
    return image;
  }
};

Code: BFS

class Solution {
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
        int color = image[sr][sc];
        if (newColor == color) return image;
        queue<pair<int, int>> q{{{sr, sc}}};
        int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
        while (!q.empty()) {
            int x, y;
            tie(x, y) = q.front();
            q.pop();
            image[x][y] = newColor;
            for (auto d : dir) {
                int dx = x + d[0], dy = y + d[1];
                if (dx < 0 || dx >= image.size() || dy < 0 || dy >= image[0].size()
                   || image[dx][dy] != color)
                    continue;
                q.push({dx, dy});
            }
        }
        return image;
    }
};

Last update: April 1, 2022