Flood fill algorithm

Graphs Traversal Problems Medium

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image. Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.


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

Examples:

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.

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

Output: [ [0, 1, 0], [1, 1, 0], [0, 0, 3] ]

Explanation: Starting from the pixel at position (2, 2) (i.e., the blue pixel), we flood fill all adjacent pixels that have the same color as the starting pixel. In this case, only the pixel at position (2, 2) itself is of the same color. So, only that pixel gets colored with the new color, resulting in the updated image.

Input: image = [ [1, 1, 1], [1, 1, 0], [1, 0, 1] ], sr = 1, sc = 1, newColor = 0

Constraints

·  n == image.length

·  m == image[i].length

·  1 <= m, n <= 50

·  0 <= image[i][j], color < 216

·  0 <= sr < n

·  0 <= sc < m

Hints

  • "Start from (sr, sc), recursively explore all 4-connected pixels with the same color. Change their color to newColor and continue the process. Base Case: If a pixel is out of bounds or has a different color, stop."
  • "Instead of recursion, use a queue (FIFO order) to iteratively process all pixels level by level. This avoids stack overflow issues in very large images where recursion depth might be too high."

Company Tags

Alibaba NVIDIA Rockstar Games Zomato Bloomberg Seagate Technology Airbnb Freshworks eBay HCL Technologies Broadcom Etsy Twilio Intel Ubisoft Johnson & Johnson Visa Stripe Ernst & Young Reddit Flipkart Bungie Micron Technology Qualcomm Byju's Google Microsoft Amazon Meta Apple Netflix Adobe