733. Flood Fill

Flood Fill (LeetCode 733) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Graph Traversal Patterns (DFS & BFS) pattern, in the Graph DFS - Connected Components / Island Counting subpattern.

How to solve Flood Fill

Flood Fill is a DFS Connected Components problem, part of Graph Traversal Patterns (DFS & BFS). Use DFS to find connected components and count islands in grids. Flood fill and province counting problems.

Work towards the solution in these steps:

  1. Write a recursive function that paints the pixel if it's the correct color, then recurses on neighboring pixels.

The reference solution runs in O(m * n) time and O(m * n) space.

The Flood Fill solution and editorial walks through the full approach with reference code.

Problem statement

You are given an image represented by an m x n grid of integers image, where image[i][j] represents the pixel value of the image. You are also given three integers sr, sc, and color. Your task is to perform a flood fill on the image starting from the pixel image[sr][sc].

To perform a flood fill:

- Begin with the starting pixel and change its color to color.

- Perform the same process for each pixel that is directly adjacent (pixels that share a side with the original pixel, either horizontally or vertically) and shares the same color as the starting pixel.

- Keep repeating this process by checking neighboring pixels of the updated pixels and modifying their color if it matches the original color of the starting pixel.

- The process stops when there are no more adjacent pixels of the original color to update.

Return the modified image after performing the flood fill.

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

Explanation:

From the center of the image with position (sr, sc) = (1, 1) (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color.

Note the bottom corner is not colored 2, because it is not horizontally or vertically connected to the starting pixel.

Example 2:

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

Output: [[0,0,0],[0,0,0]]

Explanation:

The starting pixel is already colored with 0, which is the same as the target color. Therefore, no changes are made to the image.

Constraints:

- m == image.length

- n == image[i].length

- 1 <= m, n <= 50

- 0 <= image[i][j], color < 216

- 0 <= sr < m

Topics and companies

Topics: Array, Depth-First Search, Breadth-First Search, Matrix.

Reported in interviews at Amazon, Apple, Bloomberg, Facebook, Google, Microsoft, Palantir Technologies, Qualtrics, Snapchat, Uber.

How to practise Flood Fill on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(m * n) time and O(m * n) space.

Open Flood Fill in the code editor, or read the Flood Fill editorial for a worked solution with its approach and complexity analysis.

Where Flood Fill sits in the DSA pattern sheet

Problems related to Flood Fill

Other problems that use the same Graph DFS - Connected Components / Island Counting and Graph Traversal Patterns (DFS & BFS) techniques:

Flood Fill FAQ

What is the LeetCode number of Flood Fill?

Flood Fill is LeetCode problem 733, rated Easy.

What pattern is used to solve Flood Fill?

Flood Fill is a DFS Connected Components problem. It belongs to the Graph Traversal Patterns (DFS & BFS) pattern, in the Graph DFS - Connected Components / Island Counting subpattern.

What is the time complexity of Flood Fill?

The reference solution runs in O(m * n) time and O(m * n) space.

Which companies ask Flood Fill?

Flood Fill has been reported in interviews at Amazon, Apple, Bloomberg, Facebook, Google, Microsoft, Palantir Technologies, Qualtrics, Snapchat, Uber.

Preparing this workspace.