733. Flood Fill LeetCode Solution

In this guide, you will get 733. Flood Fill LeetCode Solution with the best time and space complexity. The solution to Flood Fill problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Flood Fill solution in C++
  4. Flood Fill solution in Java
  5. Flood Fill solution in Python
  6. Additional Resources
733. Flood Fill LeetCode Solution image

Problem Statement of Flood Fill

You are given an image represented by an m x n grid of integers image, where image[i][j] represents the pixel value of the image. You are also given three integers sr, sc, and color. Your task is to perform a flood fill on the image starting from the pixel image[sr][sc].
To perform a flood fill:

Begin with the starting pixel and change its color to color.
Perform the same process for each pixel that is directly adjacent (pixels that share a side with the original pixel, either horizontally or vertically) and shares the same color as the starting pixel.
Keep repeating this process by checking neighboring pixels of the updated pixels and modifying their color if it matches the original color of the starting pixel.
The process stops when there are no more adjacent pixels of the original color to update.

Return the modified image after performing the flood fill.

Example 1:

Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
Explanation:

See also  187. Repeated DNA Sequences LeetCode Solution

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 horizontally or vertically connected to the starting pixel.

Example 2:

Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0
Output: [[0,0,0],[0,0,0]]
Explanation:
The starting pixel is already colored with 0, which is the same as the target color. Therefore, no changes are made to the image.

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n^2)
  • Space Complexity: O(n^2)

733. Flood Fill LeetCode Solution in C++

class Solution {
 public:
  vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc,
                                int newColor) {
    dfs(image, sr, sc,
        vector<vector<bool>>(image.size(), vector<bool>(image[0].size())),
        image[sr][sc], newColor);
    return image;
  }

 private:
  void dfs(vector<vector<int>>& image, int i, int j,
           vector<vector<bool>>&& seen, int startColor, int newColor) {
    if (i < 0 || i == image.size() || j < 0 || j == image[0].size())
      return;
    if (image[i][j] != startColor || seen[i][j])
      return;

    image[i][j] = newColor;
    seen[i][j] = true;

    dfs(image, i + 1, j, std::move(seen), startColor, newColor);
    dfs(image, i - 1, j, std::move(seen), startColor, newColor);
    dfs(image, i, j + 1, std::move(seen), startColor, newColor);
    dfs(image, i, j - 1, std::move(seen), startColor, newColor);
  }
};
/* code provided by PROGIEZ */

733. Flood Fill LeetCode Solution in Java

class Solution {
  public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
    boolean[][] seen = new boolean[image.length][image[0].length];
    dfs(image, sr, sc, seen, image[sr][sc], newColor);
    return image;
  }

  private void dfs(int[][] image, int i, int j, boolean[][] seen, int startColor, int newColor) {
    if (i < 0 || i == image.length || j < 0 || j == image[0].length)
      return;
    if (image[i][j] != startColor || seen[i][j])
      return;

    image[i][j] = newColor;
    seen[i][j] = true;

    dfs(image, i + 1, j, seen, startColor, newColor);
    dfs(image, i - 1, j, seen, startColor, newColor);
    dfs(image, i, j + 1, seen, startColor, newColor);
    dfs(image, i, j - 1, seen, startColor, newColor);
  }
}
// code provided by PROGIEZ

733. Flood Fill LeetCode Solution in Python

class Solution:
  def floodFill(self, image: list[list[int]],
                sr: int, sc: int, newColor: int) -> list[list[int]]:
    startColor = image[sr][sc]
    seen = set()

    def dfs(i: int, j: int) -> None:
      if i < 0 or i == len(image) or j < 0 or j == len(image[0]):
        return
      if image[i][j] != startColor or (i, j) in seen:
        return

      image[i][j] = newColor
      seen.add((i, j))

      dfs(i + 1, j)
      dfs(i - 1, j)
      dfs(i, j + 1)
      dfs(i, j - 1)

    dfs(sr, sc)
    return image
# code by PROGIEZ

Additional Resources

See also  1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.