Flood Fill

Easy

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.

Flood Fill Example 1

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]]

Flood Fill Example 2

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]]

Flood Fill Example 3

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

What should be the new image after flood filling pixel (0, 0) with the color 2? image = [[0, 1, 0, 0, 2], [0, 2, 1, 1, 2]]
image = [[2, 1, 0, 0, 2], [0, 2, 1, 1, 2]]
image = [[2, 1, 0, 0, 2], [2, 1, 1, 1, 2]]
image = [[2, 2, 0, 0, 2], [2, 2, 1, 1, 2]]
image = [[2, 1, 0, 0, 2], [2, 2, 1, 1, 2]]

Login or signup to save your code.

Notes