Hi, everyone, welcome to this new video.
So in this video, what we are going to do, we are going to write the code for the rotten oranges and
we discussed that we can use the algorithm to find out the minimum number of minutes because if it gives
you the shortest path.
Right, so we can copy the code.
We have written before a code a lot of times, so we can copy the code from here.
So let's copy this.
Let's start here.
Right, so I need X, Y and time instead of distance, let's call it time.
Time is more stable here, right?
Now, let's compare this part.
Let's start here.
So let's talk about the first line so we do not need the first line here in this public statement.
So let's remove this.
Now I can move on for direction, right?
I can move in for direction and not wait.
So let's change it to four to four.
I cannot move diagonally.
So remove all the diagonal change in X and Y coordinate.
So I need to remove this also.
Similarly, remove this and what else.
So this will get removed.
This will get removed.
And this.
Right.
So these are total four directions in which I can move.
Yep, now this is Ambrose Animatrix and Gross and great, so this will be M, which is great Nazis and
for finding out the value of and it will be good enough.
Zero Nazis.
Fine.
We need to create one visitor today.
Ambrose and and columns.
Initialise with false Guiyu of cell and no.
So what is the source of our text.
So what will be the source of our text.
I think what we need to do here is we need to push, we need to push all oranges.
Right.
We need to push Alderton oranges in the queue.
So if this is your matrix and these are revalues, let's say this is true.
This is true.
And this is to Andrest, all the values are zeros and ones.
So we are not interested in zeros.
So let's write ones.
This is one one one, one letter, one one here, one here, one here, one here.
And list.
All values are zero rate.
So we need to do we need to push all the indexes that are containing two.
So we need to push this.
We need to push this.
We need to push this right.
We need to push all of them.
So since we need to push all of them.
So what do we need to get rid of the great rate we need to iterate.
So let's add rate over the grid.
So I call zero, I list an M I C++.
Let me remove this.
Uh, yep, so we need to hydrate, so J equals zero G less than n g plus plus to F this is a rotten
orange.
So F greater Fiji is equals two to two means rotten.
So if this is right, then you need to push this.
We will push this.
So what is X coordinate I jacarandas zero and time.
Time is zero.
Right.
We are starting and since we have inserted in the queue what will do we will mark.
This has visited surveys of iji is true.
Right, so we have done these two things, so let's remove them from here.
Hmm.
Now, let's move ahead, so while the queue is not empty, did our friend correct, Pop right.
And see here, I think we need to remove this condition.
We do not need this condition here because there is no destination.
I don't have any destination as of now.
Right.
We do not have a destination cell, so we do not need this condition.
So I need to we need to remove this condition.
So let's remove it.
Let's remove this condition,
I can move in forward direction from the given cell.
So let's move in for direction X should bigger than zero and X should be less than M, Y should bigger
than zero.
Y listening and grid of X, Y should be one.
Right.
We are only interested in the vertices, which is containing one.
See, this is too.
So what I am saying, I can move here, I can move here, I can move here and let's say this is zero,
so four zero.
This is empty cell.
We are not interested in empty cell.
We are only interested in once.
So this is one what I will do.
I will write this.
I will make it to.
OK, so if the adjacent cell is containing a fresh orange and that cell has not been visited, then
visit them and in certain right.
So we need to.
What we need to do, we will push in the X, Y, and this is actually time, so time plus one rate and
visited off X Y is true.
And what we need to do, we need to mark that.
This cell has been this fresh, orange has been returned.
So this is too late.
This was a fresh orange.
Now this is a rotten orange.
Right.
We need to mark this.
Now the question is how we can find out that time, right.
That time to rot all the oranges.
So let's say I have this variable time.
Time is let's say it is zero.
So virtually with the time taken to rot, all the oranges, I think the time will be the maximum of
the current time and the time they can to rot.
All the oranges sorted all time, right?
Are you able to understand this line?
Our time is mix of time and time.
See what we need to do, what we are storing in each cell, what we are studying, etc. We are storing.
I got it at that time.
So what is this time?
What is this time?
This is the time taken to write this particular index.
Right?
This particular fresh orange.
So what I am doing, I am taking Maksym off.
All right.
We need to take maximum rate.
I need to find out the time to counteract all the oranges.
So time taken to rot, all oranges.
By the time this will be maximum time to rot
fresh orange to rot.
Any fresh orange.
Right, so time they can buy all oranges will be the maximum time to rot any fresh orange.
So that's why we need to take some here and not a minimum medium.
But if you put them in them then they will always be zero.
You should write maximum here.
You must try to make some here.
So if this thing is not clear, let me try to explain you one more time.
Si.
Let's take this example.
This is one to one.
Let's say I have one here and similarly zero zero zero litigiousness two.
This is one one and one.
And this Lazaro's.
Right.
So let's consider this case.
So initially, the value of time is zero.
But that is correct.
So this is to this will not all its corresponding oranges.
So it will read this.
It will read this.
Right.
So this cell.
Right.
This cell is containing the energy and the time time taken to write.
This is one minute.
Similarly, what the cell will contain in the exchange and the time taken to write this one time taken
was one minute late.
And similarly, in that one minute, this will also be written.
So IGY and again went right to this particular cell.
So this will become two.
Now, after one more minute, what will happen?
So this two will draw this one, right?
So it will write this one.
So IJA and time to convos this time plus one.
So time taken is two minutes for this.
Now the system similarly this disproval, this one.
So it will become two and this is IJI and time taken is two minutes right now after one more minute
what will happen.
This Trouville.
This.
So this will get Roatan and time taken is IJI and three.
Right.
So what I am doing, I am taking maximum of all these.
I am taking maximum of all.
I am taking here maximum of oil.
So the output will be three.
Right.
So that is what I am doing here.
So I hope I am clear now finally what you can do.
Can I write time here.
Can I directly written the answer time.
No, you cannot do this.
First you need to check whether there is any fresh orange in the grid or not.
Right.
First you need to check whether all the fresh oranges have been brought in or not.
So if the grid contains any one fresh orange, that means I am not able to get all the fresh oranges.
I will return minus one
before returning your answer time.
What we need to do, we need to first check whether I have written all the fresh oranges are not so
for checking, I need to hydrate.
So I am hydrating.
And if there is any fresh orange present then I need to return minus one that I am not able to rot all
the fresh oranges.
So that will be the complete cold.
Let's turn our cold to see if there is any back or not.
Yes.
So now let's salmeterol cold.
So our goal is basically working fine, right?
So this is the complete code.
Very simple and very straightforward, simple algorithm.
So actually, one of my friend interviewed with Flipkart.
And what they did was, instead of asking for the rotten oranges, they said that zero represent basically,
let's say, a mosque or something like that one represent basically a noncombat patient, basically
a normal, normal person, and to represent basically a person who has covered.
Right.
So the question was find that.
And again, the question was very similar, that every one minute this covid patient will make this
normal person, also a covid patient.
And we need to find out the number of times, I mean, a number of minutes that all the persons will
become quite positive.
So this question can be asked in different, different ways.
But you should be able to figure out that this is just a variation of rotenone and just problem only.
Right.
So I will share the court with you.
And this is pretty much all that I want to discuss in this video.
So I will see you in the next one.
Thank you.