Hi, everyone, welcome to this new video.
So in this video, we are going to solve one important question, which is rotting oranges.
So in this question, we are provided with a grid.
So we have one grid.
And this grid is basically of size and McCrossin fine.
Now, this grid can contain three values, zero one or two, so zero means empty cell.
So this is not very useful to us in this problem.
Only these two values are useful one and two.
So one means fresh orange to means rotten orange.
Right.
So we have zero ones and twos.
Zeros are not important.
Let's say this is true.
This is when.
When when.
This is when.
This is when they are similarly.
I have to here.
I have one here, one here similar to I have two here, I have one here, one here and so on.
Right.
So Value Zero is not important to us.
So we can ignore that.
So every minute what will happen.
So after one minute what will happen, the rotten orange will rot all the fresh orange which are horizontally
connected or vertically connected.
So let's talk about what will happen after one minute after one minute.
This is your rotten orange, so it will rot.
This orange and this orange.
So rotten means change the value.
So this will become two and two.
Similarly, in that one minute only, this will also get rotten.
These fresh oranges will also get rotten.
Similarly, this orange will get rotten, this orange will get rotten, and this orange will get rotten.
We can rot horizontally or we can vertically straight.
So after one minute, this will be the scenario.
This is after one.
Now, let's talk about one minute after this, so one minute after this, what will happen so this rotten
orange will rot, this fresh orange and this fresh orange.
So this is the scenario after two minutes, right.
And let it rest.
All the values are zero.
Arrest all the values at zero.
So we need to return the number of minutes I will take to rot all the oranges.
So in this case, how many time I took, I took two minutes.
So too will be the answer.
Right.
So let the example which is provided to us.
So this is the example, let's see, so in this example, we can see.
This is let me right here.
This is two one one, then one one zero, then zero one and one late.
So let's talk about what will happen after one minute or so.
After one minute.
This is a road rage, so it will rot this orange, it will rot this orange right after two minutes,
what will happen?
So this too will rot this orange and it will rot this orange.
So after three minutes, what will happen?
So this two will rot this orange.
Right.
So after four minutes, what will happen?
These two will rot this orange.
Now we do not have any fresh orange.
So how much time I took to not all the oranges for ten minutes.
So that's why the output is four.
Great.
So let's take one more example, which is give an indication.
Let's talk about this one, right, so I have two one one zero one one one zero one.
So what will happen after one minute?
This two will brought all the oranges which are horizontally or vertically connected.
So it will become too that after two minutes, what will happen?
This will rot, this orange and this orange.
Correct.
After three minutes, what will happen?
These two will rot.
This orange.
Correct.
So after four minutes, it will happen.
This will rot.
This orange.
Correct.
And that's it.
Right.
That's.
So in this we have one fresh orange.
That means I'm not able to write all the oranges.
That's why the output will be minus one.
Right.
I'm not able to write all the fresh oranges.
Fresh oranges are still present, so the output is minus one.
Now, let's talk about this.
So this is very simple.
012, right?
So you can see there are no thresholding.
There are no fresh oranges.
So how much time I will take to get all the fresh oranges zero.
Ditto because fresh oranges are not there, right?
Now, let's discuss one last example, let's say I have this two, one one and I have like two one two
zero and I have zero one and one said this is the example.
Right.
So how much time I will take to write all the fresh oranges.
So let's talk about after one minutes after one minute what will happen.
These two will write this fresh orange and this fresh orange.
Similarly, in that one minute only these two will also write.
So it will read this one.
Right.
So this is the scenario after one minute.
So what I want to say is all the rotten oranges will rot, all fresh oranges.
Great.
And we can have multiple rotten oranges.
We can have multiple rotten oranges.
So in one minute, all rotten oranges will rot there horizontally, vertically connected, fresh oranges,
so in one minute this will be the scenario.
And after one more minute, after two minutes, what will happen?
These two will rot.
This one these two will rot.
This one, right.
So total number of time to rot.
All the oranges is two minutes late.
So this is very important to understand that in one minute, every rotten orange, every orange will
rot its corresponding oranges horizontally, vertically connected, fresh oranges.
Right.
So that is very important to understand.
I am repeating myself after every one minute, all the rotten oranges that you have will rot its corresponding
fresh oranges horizontally, vertically connected fresh oranges.
And it.
So again, what we need to do, we need to find out the minimum number of time that we need to find
out the minimum number of minutes.
That is, we can use BFC algorithm.
Right.
We can use BFE to find out the minimum number of minutes.
Right.
So how it will work.
Let's talk about this.
This is to then when one then one one zero, then zero one one.
So what I'm going to do, what I will do in the queue, I will push all the rotten oranges, I will
push all the rotten oranges.
So this is a rotten orange.
So I will push to.
That is, I will push the index zero zero and the time time is initially zero.
So after one minute, what will happen?
So this is the nod to.
And initially, right, initially zero.
So what will happen after one minute, what will happen?
This will get done and this will get rotten.
So this will become to this will become too.
So, too.
And how much time in one minute?
Only late after one minute to two and one.
Correct.
Now again, these two will draw this one and this one.
So again, this really will become two and two.
So it will become two and two.
So after one more minute, these two will write this one, so it will become two and three and this
will come to after one more minute, these two will write this one.
So this will become 24.
And what is our answer?
Our answer is for.
Right.
So that's how it will work.
Very, very simple, so we can use algorithm, we can visualize that this basically is a graph.
Eight and initially, what I will do, I will push all the rotten oranges in dequeue, so how we can
solve this problem, do you need to do you need to copy the code from here?
You need to copy the code for this problem.
Right.
And paste the code here and modify the code according to this problem.
So BFW algorithm can be used here.
So do give it a try rate.
I will write the code for this problem in our next video.
So this has allowed this device.
I will see you in the next one.
Thank you.