Flood Fill
Question
You're given a matrix with cells that contain integers. The integers represent colors, with each cell symbolizing a pixel within this "image".
Perform a flood fill starting from a given pixel coordinate and color, and return the modified image.
A flood fill changes the starting pixel's old color to a new color, then "floods" the top-right-bottom-left neighboring pixels that have the same old color with the new color. This continues until all the connected pixels with the old color have been changed to the new color.
Input: image = [[0, 0, 1], [0, 0, 2], [0, 2, 0]], (1, 1), new_color = 3
Output: image = [[3, 3, 1], [3, 3, 2], [3, 2, 0]]
Input: image = [[0, 1, 1, 0], [1, 0, 0, 1], [1, 0, 1, 1]], (2, 1), new_color = 2
Output: image = [[0, 1, 1, 0], [1, 2, 2, 1], [1, 2, 1, 1]]
Input: image = [[0, 0], [0, 0]], (0, 1), new_color = 0
Output: image = [[0, 0], [0, 0]]
Clarify the problem
What are some questions you'd ask an interviewer?
Understand the problem
Login or signup to save your code.
Uh oh... looks like you don't yet have access.
Not sure what this unlocks? Check out a free pattern section.