Hi, everyone.
So in this video, we're going to solve this question, Woodgate Matrix.
OK, so basically the government is basically engross and ammunition and what we have to do, so we
have to rotate this matrix by 90 degrees in the clockwise direction.
OK, so this is the clockwise direction and this is anticlockwise direction.
OK, so basically we have to rotate the government by 90 degrees in the clockwise direction.
OK, so let us consider some example.
So let us consider this matrix.
OK, so now that is rotated by 90 degree in the clockwise direction.
OK, so initially this was like this.
Now, after dating, so consider this line, so after dating this line with the 90 degree, so this
line.
That new line will be this one.
OK, so if this is the old line, if you can rotated by the 90 degree in the clockwise Badakshan, so
this line will become like this.
OK.
Now, let us rotate this line.
So it will look like this, I have three and one.
OK, so in this way, this line will become this.
So this will be three and this will be one.
Initially, this was three and this was one.
OK, now let's rotate this lane to rotate in the clockwise direction by 90 degrees so it will become
full comma, too.
OK, so this is the new matrix, three one for Commodore.
OK, so after rotating this matrix, this is the new matrix.
OK, now US did this matrix.
OK, so now let us rotate this matrix.
So this is.
One four seven, so after reading this line, so after reading this line in the clockwise direction
where 90 degree, so this will be the new this will be the new line.
So it will be seven for one seven four one, OK.
Now, let us read this line.
So this this will become basically eight, five and two.
And this is nine, then six and then three.
OK, so this is the new matrix.
Simple.
Now, let us read this matrix.
OK, so what.
The first rule.
So.
Let us rotate this lane.
So it will be 15, then 13, then two and then five.
The second will be basically 14, then three, then four and then one, that third row will be, well,
six, eight and nine and the photo will be sixteen, then seven, then 10 and then 11.
OK, so this is basically the new matrix, OK, this will be the new matrix, so what we have to do,
so we have to convert this matrix, we have to convert the input matrix into the new matrix.
OK, the matrix, which is rotated by 90 degrees in the clockwise direction.
OK, OK, so now let's discuss how we can solve this equation.
OK, so this is a very simple question.
OK, so let us consider one example.
So suppose this is the Matrix.
One, two, three, then I have four or five, six and then I have seven, eight, nine.
OK, so now rotate this matrix by 90 degree in the clockwise direction.
So what with a new matrix.
So new matrix will be basically seven for one, then eight five two and then nine six three.
OK, so what will do?
So our approach is very simple.
I have to perform two step.
So first step is basically take transpose of the given matrix and the second step is after transpose
what we will do.
Basically we will reverse each rule.
OK, reverse each rule.
OK, so how I come to know that after being transferred and then reversed.
So this is basically observation.
OK, so this is basically observation if you will see the examples carefully, if you will observe carefully.
So I have to perform two steps.
First, I would take transpose and then I have to reverse each rule.
OK, so let us take its transpose.
So first step is basically taking transpose so and transpose what we will do.
We will swapper win column.
OK, so transpose means basically you will swap rows and columns.
So let us take the transfer so it will become one, two, three, then I have four, five, six, then
I have seven, eight, nine.
OK, so this was one, two, three.
Now this is one, two, three.
This is four, five, six.
Now this is four, five, six.
So this was seven, eight, nine.
Now this is seven, eight, nine.
OK, so transport's taking is very, very simple, but you have to do so.
I have to strap Ruen column.
OK, so if this was the rule.
Now, make it clear if this was the second rule, make it second column, if this was the third rule,
then make it third column.
OK, very, very simple.
We are just swapping rules and columns.
OK, now the second step, basically, I have to reverse each rule.
So now let's reverse this first rule.
So it will become seven for one.
Now let us reverse the second rule.
So it will become eight five to reverse the third rule.
So it will become nine six three.
OK, and you can match.
So they both have same.
OK, so if you follow these two steps first taking place first and then reverse each row, we will be
able to soil our question.
OK, now let's take one more example if you want to understand it.
OK, let's take one more example.
So basically, let's take this one example, the example which is given in the question.
OK.
We just have to follow two steps, transpose and then reverse each.
OK, so let us take the transpose.
The first step, first step is basically taking the task force.
Big task force means we're perusing columns.
So if this is the first rule, I will make it first column.
So this is five one nine 11.
This is the second rule.
Make it second column so it will become two four eight.
And then this is the third rule.
Make it third column.
So 13, three, six and seven.
This is the fourth rule.
Make it fourth column.
OK, so this will be 15, then 14, then 12 and then 16.
OK, so after this, after the first step, what is the second step to second steps is reverse each
rule.
OK, we will reverse each rule.
Every single rule will be reversed.
OK, so now let's reverse this little so it will be basically 15, then 13, then two and then five,
now reverse this rule.
So this is 14, then three, then four, then one reversed this rule.
So it is 12, then six, then eight, then nine.
Now reverse this rule.
So this will be 16, then seven, then 10 and then 11.
OK, now you can match.
OK, so they both have some.
OK, they both have him, so basically our goal is going to be look very, very simple.
First, I will write the code for taking the transpose, which is nothing, just rows and columns.
And the second is basically I will reverse the Egil.
OK, now what will be the time and the space complexity?
So first, I am digging transport.
So how much time it will take?
So simply it will take into account the dimensions of the matrix that and so I have to go through each
and every element.
So it will take an enduring time and then I have to reverse each rule.
OK, so what I will do, I will go through each and every rule and I will take the reverse.
So bless and doing OK for reversing each rule.
So basically, if you will add these two sort time complexities basically and square.
And the space complexities basically are drawn, OK?
We are we will not use any extra space.
So this is the time and the space complexity for the solution.
OK, digging transports and reversing each little.
OK, so now let us write the code if you understood the logic.
OK, so the government has basically passed by reference, OK, so we have to do changes in the government
only we do not have to create new metrics.
OK, we have to solve this question in place.
So the meaning of this is basically the changes in the same metrics.
OK, do not allocate any extra space.
So now let us write the code.
So first let us find out how many rules and columns are present.
OK, so.
Let's call it Immonen.
OK, so this is basically tech, Start says.
And what is and so this is metrics of zero Dorseys.
Now, after finding out this is what you have to do, so basically first step, we have to take the
transport.
And passement means sabras in columns.
OK, we will swap roles in columns, take the transpose.
OK, so now let us write the code for taking the task force.
Zero equals zero.
Is M Andrew Placeless?
Then column so column will start from the.
Then Collum less than N and then column placeless.
Now, what we have to do, so basically I have to strap rows and columns, so this is basically a matrix
of.
Column, Animatrix of column.
OK, very simple.
So basically at this point.
Our task force is done now what we have to do.
I have to reverse each second step.
So the second step is basically.
The reverse is true.
OK, so what we'll do, I will go through each and every rule and I will reverse them.
So I have to go through each and every rule, so I loop for going through each and every rule.
Total Ambros out there.
OK.
Now, how to reverse so we know how to reverse.
OK, so considering that now, if you want to give us an idea what we will do, it will take two point
that you have a start point out here and you will pick and point out here.
And similarly what you will do, you will start point and point to the starting point and move in this
direction and move in this direction.
So we will able to start placeless.
I will do anything minus minus.
I will continue.
This thing will basically start is less than ideal and or you can write and will start this Lieutenant.
OK.
Now, let us create a start and end point, so the start point that will be at zero position.
And that will be out basically and minus one politician, OK, and is the number of columns.
Now, while it's this list than ideal Westwind, what do you have to do, you will slap.
OK, so I have to step.
Railways fixed, the value of roads fixed.
OK, I'm reversing that.
So start and similarly mattocks of is fixed basically and the column is variable, so it will be end.
OK, I'm scrapping the start in the end.
Point out what I will do.
I'm I will do start placeless slightly.
Move ahead.
I will do end minus minus.
OK, now our second step is also done.
OK, so our first step taking transpose, our second step reversing the arrow.
Now I think our goal will work fine.
OK, now let us try to run our code and then we will try to submit.
OK, so we are getting wrong on, so that means we did something wrong.
Let's see.
OK, so the mistake is basically.
I forgot to initialize a rule.
OK, so what is happening here is if you will see it carefully.
So if this is the matrix, so I want my taxes, particularly one, two, three, then four, five,
six, and then seven, eight, nine.
OK, now let us take the transport.
So it starts both will basically be one, two, three, then four, five, six, and then seven, eight
and nine.
OK, so our first rule is basically one four seven.
So this is one four seven.
Then our second rule is basically to five, eight to five it then our third rule is basically three
six nine.
So this is the third rule.
OK, basically what we are doing, our reverse function is not locking.
OK, our rivers function is not working because you can see here I forgot to initialize it all with
zero.
OK, will be initialized with zero.
So now let us do the correction.
So will be initialized to zero.
OK, now this is correct, after initially said over zero, so now Ladson.
OK, so our court is working fine now let us strive to submit our good.
OK, so our goal is working fine.
Now, as discussed, what is the time Diamond, the space complexity, so we know the time complexity
is basically order off and then you can say and square because it is a given that the dimensions are
increasing, OK, and the space complexity is basically ordered offline.
OK, now let's discuss some good examples.
So here in discussion.
Let us talk about some more informations.
OK, so the given question is basically so if we have to rotate the metrics by 90 degrees clockwise
direction, so that means plus 90.
OK, so 90 degrees clockwise direction, so clockwise I'm taking place.
OK, so we have to rotate by plus degree, rotate by plus 90 degrees.
Then the approach is very simple.
First, big the transport and then reverse each.
OK, big transports and reverse each.
Now, if the question is basically rotate by minus 90 degree, if this is the question, rotate by minus
90 degrees or minus means in the anticlockwise direction.
OK, so what I will do, I will take the transport.
And then basically, I will reverse each column.
OK, so instead of reversing each rule, I will reverse each column.
Simple.
Now if the question is basically rotate by plus 180 degrees, that means 180 degrees in the clockwise
direction.
So there are two ways to solve this question.
OK, so the first rule is basically what I will do.
So I will follow this approach.
I will rotate by plus 90 degree.
I will rotate where plus 90 degrees and I will perform this step two times.
OK, I will perform the rotation by 90 degree two times, OK, rotation by 90 degrees two times.
So it will be plus 180 degrees.
Now, the second step basically is very simple.
What we will do, you will reverse.
So basically there are two ways.
So this is the first.
We rotate by 90 degrees two times.
Secondly, to solve this question is basically reverse each rule.
And also the first column was each row and reverse each column.
OK, now the fourth type of problem can be I have to rotate by minus 180.
That means 180 degrees in the anticlockwise direction.
OK, so again, there are three steps to solve this problem.
OK, so basically there are three ways to solve this problem.
So first rate is basically what I will do.
I will rotate by minus 90 degrees and I will do the rotation by minus 90 degrees two times.
OK.
I will rotate by minus 90 degrees and I will do it two times, so in total it will be minus 180 degree
simple.
Now, the second way is basically what you can do.
You will reverse each column and then basically you will reverse each rule.
OK, so this is the second we.
The third way is basically rotate by minus 180 degree is basically the same as.
Rotate by plus 180 degree.
OK, so the AC.
OK, so if you will rotate by minus 180 degrees or you will rotate way plus 180 degrees.
Basically, they both are saying, OK, you can take an example to see yourself.
OK, so these these are four types of problem.
If you have to rotate by plus 90 degree that we have already solved, transpose and reverse each little
second is basically minus 180 degrees, again, transposed to reverse each column.
That is basically the third type of question can be rotate by plus 180 degrees.
So the first rate is always this one plus nine to the rotation by plus 90 degree and make it two times.
OK, the second one is basically you will reverse each rule and reverse each column and minus 180 degrees
again, but from the rotation by minus 90 degrees two times or the second way to basically reverse each
column, then reverse each row.
And third, which is basically rotation by a minus 180 degree or rotation by plus 180 degree, they
both are saying, OK, now plus means clockwise direction, minus Miss Anticlockwise Direction.
OK.
OK, so this is not anticlockwise, this is anticlockwise, OK, so I remembered.
So this is not clockwise.
This is not anticlockwise.
Anticlockwise is this one.
And similarly, so this is minus.
So this is anticlockwise.
OK, sorry, I'm very sorry.
So this is minus decis anticlockwise.
OK, finally, this is clockwise in the direction of glogg and this is anticlockwise opposite to the
direction of Gloc.
OK, so if you have any doubt in this question, you can ask me.
OK, one more thing.
The time, complexity and all the solution will be emon.
And the space complexity will be open.
OK, we are just traversing through each and every element.
OK, so thank you.