Here is the interview question prompt, presented for reference.
Imagine the implementation for the paintbucket feature in Microsoft Paint, or how certain image filters work. You click on a specific pixel on a screen, and every similar-colored neighboring pixel would change to your selected color. Let's implement a simpler version of that feature.
You're given a n x n
matrix array of numbers. Let's say you'd like to replace all similar and connected cells of a certain number with another, starting at column 0
and row 0
.
So here's your input:
const input = [
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 0],
[1, 0, 0, 1, 1, 0, 1],
[1, 2, 1, 2, 1, 2, 0],
[1, 2, 1, 2, 2, 2, 0],
[1, 2, 2, 2, 1, 2, 0],
[1, 1, 1, 1, 1, 2, 1]
]
And the final result of starting at column 0
and row 0
would be as follows:
// floodFill(matrix, startingRow, startingCol, newValue)
floodFill(input, 0, 0, 3);
// [
// [3, 3, 3, 3, 3, 3, 3],
// [3, 3, 3, 3, 3, 3, 0],
// [3, 0, 0, 3, 3, 0, 1],
// [3, 2, 1, 2, 3, 2, 0],
// [3, 2, 1, 2, 2, 2, 0],
// [3, 2, 2, 2, 3, 2, 0],
// [3, 3, 3, 3, 3, 2, 1]
// ]
Notice how all the 1
s that were bordering the original 1
in column 0
and row 0
have been transformed to 3
.
0-indexed
|V|
, |E|
represent the number of vertices and edges of the graph (if you consider the matrix as a graph)O(|V|)
O(|V|)
considering the call stackYou can see the full challenge with visuals at this link.
Challenges • Asked almost 7 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Flood Fill Paintbucket.