Skip to content
Decoder

Flood Fill

Easy

Problem

An image is a grid of integers, one per pixel. Starting from the pixel at row sr, column sc, repaint it with color, then keep spreading to every pixel that shares the starting pixel's original colour and touches an already repainted pixel up, down, left or right. Return the image after the fill.

Example 1

image = [[1, 1, 1],

Example 2

         [1, 1, 0],

Example 3

         [1, 0, 1]], sr = 1, sc = 1, color = 2

Example 4

  →  [[2, 2, 2],

Example 5

      [2, 2, 0],

Example 6

      [2, 0, 1]]   (the bottom-right 1 only touches the region diagonally)

Example 7

image = [[0, 0, 0],

Example 8

         [0, 0, 0]], sr = 0, sc = 0, color = 0

Example 9

  →  unchanged    (the new colour is the old colour)

Constraints

  • 1 <= image.length, image[i].length <= 50
  • 0 <= image[i][j], color < 2^16
  • 0 <= sr < image.length and 0 <= sc < image[0].length

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.