 What's up guys, my name is Michael and welcome to my YouTube channel today. We're going to do another lee code challenge called a flood fill Basically you're a give an image is represented by a 2d array of integers each integer representing the pixel value of the image You're given a coordinate, which is the row and column representing the row and column of the flood fill and a pixel pixel value called new color so our job is to perform a flood fill which is Considering the starting pixel and you're going to go through for connected directionally to the star pixel of any star pixel plus any Direction connected forward directionally to those pixels also the same connection of star pixel so on So our job is to replace all the color of the aforementioned pixels with a new color and then we're going to return the modified image So in this case we have image which is 1 1 1 1 1 1 0 1 0 1 So this represents the column Each column like this is like a matrix of values. So like 1 1 1 1 1 0. So like let's I'll draw this out To make it less Ridiculous. So image is 1 1 1 1. So that's 1 1 1 1 1 0 101 okay, so this is our image It's basically a matrix of values we're going to start at SRC SR which is row one and then column one and our job is to replace Every value of the row one and column one with the new value of color two So what is row one and column one? That is to do row one column one row one Wait, are they basing off zero indexing? Let's see Are they basing off in zero indexing? Okay, I'm gonna assume they are from the center with position One one. Okay. Yeah, so they they're starting with the center. So one okay So here's I'll just show you the index is one zero one two and then the index is zero one two and what they're starting at is SR one SC one so this the middle one one column one row one Row one column one. That's what they're doing and they're starting here and Now their job is to basically replace all the values between These values with the that are not colored two in four way. So They're awesome, they're not considering zero, right Yeah, okay, so in forward directionally this would what is above this is gonna become two This is gonna become two This is gonna be on two Uh They don't count zeros for some reason, but okay, so one two Did they did the bottom become two? Yeah, okay, so this bottom becomes two Let's see the top Diagonally becomes two also. Yeah, and then the right side becomes two This this bottom didn't become two for some reason. Let's see No, the bottom corner is not colored to because it is not four directionally connected Four directionally connected Okay, so they're saying that What does that mean by that? Why did this bottom not become two? Oh, yeah, lucky. I got to go back and read it this again consider starting in a connected forwarded Four directionally to the starting pixel the same as plus any four directionally to the Um What does that mean it is not four directionally connected? I'm guessing that means is that They have to be surrounded Four directionally. So here from the middle. This was surrounded by four one two three four So that's why these became two. This is surrounded by four one two three four And that's why these became two one two three four. So that's what they mean by four directionally. That's what I think okay, so That's basically the gist of the algorithm of the problem. So now we're gonna actually code this up So first of all We know flood fill is gonna return the same image. So we're gonna return image Next we have to do depth first search. So how do we do that? You have to create a new method called depth first search depth first search What is that first search gonna have it's gonna have the source the row column and then it's gonna have the new color And then it's gonna have the same image as well So I'll just copy this Okay, um What are they gonna do? So So first we have to get the current color So that's just gonna be current color and this is gonna equal to the image at the row and the column Um, then we have to check if current color is not equal to the new color Then we have to run depth first search on The image the the row the column and then We need to pass in the new color also Uh, the current color. So here I'm gonna do current color current color And then new color Okay, so We're gonna run depth first search on that and this this depth first search is gonna change the current color to the new color Okay, so How do we do that? So what we're gonna do is we are going to change the current color to the new color So to change the current color to the new color all we have to do is just uh Let's see if current color is equal to new color No, no, no We get the No, that's not what we do. Okay, um We have to do image at r and c is gonna equal to new color Then we have to run depth first search going through the four ways which is up down left and right So to do that we're going to do do do do do if r is greater than equal to one That means we have to go So if we're on the first row Like we're greater than the first row. We're gonna Go up. This is gonna go up. Okay, so we're gonna pass in depth first search Going up. So we're gonna do this but then we're gonna do Do row minus one Same color Everything is also the same So this is gonna go to the left side Uh, then we have to check if Uh, let's see if r is less than the size of the image Uh Size of the image minus one. Yeah, so then that means we're gonna go Down Yeah, that means we're gonna go down So that means we have to do dfs image and then go down The reason i'm checking the Reason why i'm checking this and this is to make sure that we're not going out of bounds when we check The images so This is gonna go down No, this is this is gonna go up because row minus one means we're going up by one row. So and remember the picture Zero one, right? One is the row one if I want to go up. I subtract one from the row So that'll be add zero that's up row if I want to go down I have to do my current row one plus one which is two and that goes down And this is just to make sure that I don't go out of bounds between the size and this make sure I don't go Below zero so Okay, then we're gonna do the same thing and then what we're gonna do is we're gonna pass in um We're gonna pass in the value of Column there's a there's a large ambulance my Thing sorry about that guys. Okay, so we're gonna pass in the column and we're gonna pass in these This is gonna be the same thing as a row, but we're gonna Subtract one here and then we're gonna do the same thing for uh less than image dot size minus one Uh, this is gonna be plus one. Okay Uh, and then I think that's it pretty much Uh, let's see. What else do we have to do? All right, so one thing that I did forgot to do is that Remember we passed in the current color here into the dev first search So the reason why we did that is because we had wanted to check if the current color Uh all is equal to Whatever the new color that we pass in right so in this case In this example, we had to make sure they're all the we got the current value that we're starting at which is one And we had to make sure that all the surrounding areas of one Of whatever new color that we have is equal to one and then we're gonna change it so Current color is actually the starting color that you started at So we actually have to check if our starting color is equal If If the color that we're currently on is equal to the starting color So current color is actually the starting color. I should rename that but Because if it's equal to the starting color, then we have to do all this stuff We have to go dev first and go through like check every every value because like if it's equal to one, right? So like let's say I'm on here one If this is equal to one, then yeah, then I would change it But in this case zero zero is not equal to one. So that's where we didn't change zero. So that's why we have to check if the the color that we're on currently is equal to our Starting color that we had current color is actually starting color, but yeah That's we have to do this Um, I'm gonna just indent this real quick And then we hit submit. Let's see if it Does anything Still runtime error. What is the problem? Let's see. What is our problem? Image dot size this should be right Something without a bounds I think less than or equal to No plus one is less than the size Oh, maybe column it has to be zero No, it's that's not right What is our problem? Oh, so okay, so this actually has to be plus. Let's see Okay, okay. So the problem was is that here when we we were decreasing the row even if uh When r is less than the size minus one what we should have done is plus one because this is actually going Down this is going down and we're checking if it reaches the end of the image size for the boundary So we shouldn't have subtracted one. We should we had to add one. So yeah That's basically how you do this question. You had to go through You get the current color that you're on and then you That first search on that color and then you have to keep checking if it's the same color That as your current color and then that's how you would that's how you would solve this question And then each time you check it you have to increase one for like going to above below To the left and then to the right and you have to make sure it's between the boundaries Yeah, that's how you do this question. Great. Come subscribe. I'll check you guys later. Peace