Flood Fill Algorithm
In this class, We discuss Flood Fill Algorithm.
Readers can prepare an entire competitive coding course to crack product development companies. Click Here.
The reader should have basic coding skills to work with competitive coding. Click here.
Question:
We represent an image as a two-dimensional array of integers.
The pixel values are in the array.
Input: Given row and column value for one of the pixels and new pixel value.
Flood Fill: Convert the starting pixel colour to a new colour.
Take the pixel UP, DOWN, RIGHT, and LEFT from the starting pixel.
If any of the four pixels have the same starting pixel colour, change the pixel value to a new colour.
Continue the process to change the possible pixels.
Example:
The below diagram shows an example of input and output.
The starting pixel row and column values are [4, 1].
New color = 2
Time complexity: O(N X M)
Space Complexity: O(N+M)
Logic:
We can do this using recursive functions and using Loops.
Each function will call four functions to check the UP, DOWN, RIGHT, and LEFT pixels.
Using loops, we can use a queue data structure to store the coordinates.
We provided a step-by-step explanation in the video.
Code: