Advertisement

Flood Fill - LeetCode 733 Solution

Flood Fill - Complete Solution Guide

Flood Fill is LeetCode problem 733, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 horizontall

Detailed Explanation

The Flood Fill problem asks us to simulate filling a connected region of an image with a new color. We're given an image represented as a 2D array (matrix) of integers, a starting pixel coordinate (sr, sc), and the new color to fill with. The goal is to change the color of the starting pixel and all its directly adjacent (horizontally or vertically) neighbors that have the same original color. This process repeats recursively for the newly colored neighbors until no more adjacent pixels with the original color can be found. The final modified image should be returned.

Solution Approach

The provided solutions utilize Depth-First Search (DFS) to traverse the connected component of pixels with the same color as the starting pixel. The algorithm begins at the given starting pixel (sr, sc) and recursively explores its four adjacent neighbors (up, down, left, right). For each neighbor, it checks if the neighbor is within the bounds of the image, and if its color matches the original color of the starting pixel. If both conditions are met, the neighbor's color is changed to the new color, and the DFS process is recursively called on that neighbor. This continues until all reachable pixels with the original color have been colored with the new color.

Step-by-Step Algorithm

  1. Step 1: Get the original color of the starting pixel image[sr][sc].
  2. Step 2: If the original color is the same as the new color, return the image immediately (no changes needed).
  3. Step 3: Define a recursive DFS function that takes the current row and column indices as input.
  4. Step 4: Inside the DFS function, check the base cases: if the current pixel is out of bounds or if its color is not the original color, return (stop recursion).
  5. Step 5: If the current pixel is within bounds and its color is the original color, change its color to the new color.
  6. Step 6: Recursively call the DFS function on the four adjacent neighbors: (row + 1, col), (row - 1, col), (row, col + 1), and (row, col - 1).
  7. Step 7: After the DFS function completes its execution (i.e., all connected pixels have been filled), return the modified image.

Key Insights

  • Insight 1: The core idea is to identify a connected component of pixels with the same color as the starting pixel.
  • Insight 2: Depth-First Search (DFS) or Breadth-First Search (BFS) are suitable algorithms to explore and modify this connected component efficiently.
  • Insight 3: A crucial optimization is to check if the original color is the same as the new color to avoid unnecessary computations and infinite recursion in some implementations.

Complexity Analysis

Time Complexity: O(m*n)

Space Complexity: O(m*n)

Topics

This problem involves: Array, Depth-First Search, Breadth-First Search, Matrix.

Companies

Asked at: Akuna Capital, Criteo, OpenAI, Palantir Technologies, Sumo Logic.