Skip to content
Onsite

Flood Fill

EasyP2Coding

You are given a grid image of integer pixel values, a starting pixel (sr, sc) and a new color. Repaint the starting pixel and every pixel connected to it through up, down, left or right moves that share the starting pixel's original color. Return the modified image.

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]]
Why
The bottom-right 1 is only diagonal to the region, so it keeps its color.

Example 2

Input
image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0
Output
[[0,0,0],[0,0,0]]

Constraints

  • 1 <= rows, cols <= 50
  • 0 <= image[i][j], color < 2^16
  • 0 <= sr < rows, 0 <= sc < cols

Hints

Run checks the 3 visible tests, Submit all 9.