101 Logo
onenoughtone

Solution Approach

Solution Overview

There are 2 main approaches to solve this problem:

  1. Recursive DFS Approach - Complex approach
  2. Iterative BFS Approach - Complex approach

Approach 1: Recursive DFS Approach

The recursive Depth-First Search (DFS) approach is a natural fit for the flood fill algorithm. Starting from the given pixel, we recursively explore all adjacent pixels of the same color, changing them to the new color as we go.

This approach is intuitive because it directly models how the paint would "flow" from the starting pixel to all connected pixels of the same color.

Algorithm:

  1. Define a recursive function floodFill(canvas, row, col, originalColor, newColor) that takes the canvas, current position, original color, and new color as parameters.
  2. Base cases: If the current position is out of bounds, or the current pixel is not the original color, or it's already the new color, return without doing anything.
  3. Set the current pixel to the new color.
  4. Recursively call floodFill on all four adjacent pixels (up, down, left, right).
  5. The main function should initialize originalColor to the color of the starting pixel and then call the recursive function.

Time Complexity:

O(m × n)

In the worst case, we might need to visit all pixels in the canvas once, where m is the number of rows and n is the number of columns.

Space Complexity:

O(m × n)

The recursion stack can grow up to the size of the canvas in the worst case (e.g., if the entire canvas is filled with the same color).

Approach 2: Iterative BFS Approach

An alternative approach is to use Breadth-First Search (BFS) with a queue. This approach processes pixels level by level, starting from the given pixel and expanding outward.

While this approach is less intuitive than the recursive DFS, it has the advantage of avoiding potential stack overflow for very large canvases.

Algorithm:

  1. Initialize a queue with the starting pixel.
  2. Get the original color from the starting pixel.
  3. While the queue is not empty:
  4. - Dequeue a pixel.
  5. - If the pixel is out of bounds, or not the original color, or already the new color, skip it.
  6. - Set the pixel to the new color.
  7. - Enqueue all four adjacent pixels (up, down, left, right).

Time Complexity:

O(m × n)

Similar to the recursive approach, we might need to visit all pixels in the canvas once.

Space Complexity:

O(min(m, n))

The queue will contain at most min(m, n) elements at any time, which is the maximum number of pixels in a level of the BFS traversal.

Pseudocode

solution.pseudo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function floodFill(canvas, startRow, startCol, newColor):
originalColor = canvas[startRow][startCol]
// If the starting pixel is already the new color, no need to do anything
if originalColor == newColor:
return canvas
// Call the recursive helper function
dfs(canvas, startRow, startCol, originalColor, newColor)
return canvas
function dfs(canvas, row, col, originalColor, newColor):
// Check if the current position is valid
if row < 0 or row >= canvas.length or col < 0 or col >= canvas[0].length:
return
// Check if the current pixel is the original color
if canvas[row][col] != originalColor:
return
// Change the color of the current pixel
canvas[row][col] = newColor
// Recursively fill the adjacent pixels
dfs(canvas, row + 1, col, originalColor, newColor) // Down
dfs(canvas, row - 1, col, originalColor, newColor) // Up
dfs(canvas, row, col + 1, originalColor, newColor) // Right
dfs(canvas, row, col - 1, originalColor, newColor) // Left
ProblemSolutionCode
101 Logo
onenoughtone