CodePark/ problems / Flood Fill
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Graphs and topological sort

Flood Fill

easyabout 15 minutes in an interview

image[row][col] is a grid of numbers, and each number stands for a colour. Think of a paint-bucket tool: you click the pixel at (sr, sc). That pixel, and the whole patch around it in the same original colour, turns into color.

  • The patch spreads only up, down, left and right, never diagonally. A pixel joins the patch if it has the original colour and touches the patch, directly or through other patch pixels.
  • Return the painted image. You may change image in place and return it.
  • If color already equals the original colour, the image stays the same.
  • The image is at most 50 × 50, and (sr, sc) is always inside it.
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]]The 1 in the bottom-right corner meets the patch only at a corner, so it keeps its colour.
Input: image = [[0, 0], [0, 0]], sr = 0, sc = 0, color = 0 Output: [[0, 0], [0, 0]]