26 · Graphs: BFS & DFS

#733 Flood Fill

Easysolve on LeetCode ↗

frequently reported atAmazonMicrosoftGoogle

the problem

In plain words

Starting from pixel (sr, sc), recolour it and every pixel connected to it (4 directions) that has the same original colour. Return the image.

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

out [[2,2,2],[2,2,0],[2,0,1]]

The bottom-right 1 isn't connected to the start (only diagonally).

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

out [[0,0,0],[0,0,0]]

  • 1 ≤ m, n ≤ 50
  • 0 ≤ image[i][j], color < 2¹⁶

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #200
      Number of Islands
      Mediumin Graphs
    2. #695
      Max Area of Island
      Mediumin Graphs
    3. #130
      Surrounded Regions
      Mediumin Graphs