ROTATE IMAGE | Leetcode | C++ | Java | Brute-Optimal

preview_player
Показать описание

Find DSA, LLD, OOPs, Core Subjects, 1000+ Premium Questions company wise, Aptitude, SQL, AI doubt support and many other features that will help you to stay focussed inside one platform under one affordable subscription. Have a hassle free one stop solution for up-skilling and preparing.

Рекомендации по теме
Комментарии
Автор


Understood or nott? am waiting in the comment section ;)
.
.

takeUforward
Автор

Loved the multiple approaches... After practicing 2-3 questions explained by you, I tried to solve the same problems myself, and guess what I was able to make brute force - better - optimal approaches on my own for the medium as well as hard questions...Thank you very much striver for putting that level of confidence in me... I really appreciate your work...

kaiwalya_koparkar
Автор

In Competitive Programming You're like a GOD.
Love From Gujarat.

ruchitasalekar
Автор

Understood the intution and the logic. But I felt some struggle to come to the coding logic for the transpose as well as reverse. But after few dry runs I am able to understand this.
If we consider the matrix
[ 1 2 3]
[4 5 6]
[7 8 9]
After Calculating transpose it will be like this
[1 4 7 ]
[2 5 8 ]
[3 6 9]
if we carefully observe the middle element 5 and the last element 9 and firstelement 1 are not changed.
In the first iteration we need to swap 2 and 4 (1, 0) (0, 1)
In the second iteration we need to swap 7 and 3 (2, 0) (0, 2) and (8 and 6) (2, 1)(1, 2)
So the for loop runs like this if we see in the above clearly j <i and j starts from 0 and i starts from 1.
for i:=1;i<len(arr);i++{
for j:=0;j<i;j++{
swap(matrix[i][j], matrix[j][i]
}

And for the reverse we will only need to go to len(matrix/2)
for i := 0; i < len(matrix); i++ {

for j := 0; j < len(matrix)/2; j++ {

matrix[i][j], matrix[i][len(matrix)-1-j] = matrix[i][len(matrix)-1-j], matrix[i][j]
}

}
its kind of start element and end element swapping in normal array reverse.
Do more and more dry run you will understand. I think most of the persons understood. May be anybody who is newbie like me to the cp will get struggle.

vamsikrishnasiddu
Автор

Another approach:
Rotating a matrix is equivalent to first reflection about the right diagonal, then reflection about a horizontal line passing midway to the matrix. If you consider a cartesian system on the matrix, you can easily find the resulting point after these 2 operations, using the formula for mirror image of a point about a line.
With the above, you'll get that the number at matrix[x1][y1] will go to matrix[y1][n-1-x1] after rotation.

Now that you know exactly where each value will go after rotation, you just need to find a way that you can put those values in place. For this you can go square by square inwards and replace values accordingly.

bharathhegde
Автор

is striver here" this is so cute..i always listen to this 2-3 times and playback my video for seconds

dhanashreegodase
Автор

THis also does the work -
// Top Down Reverse
reverse(matrix.begin(), matrix.end());
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
swap(matrix[i][j], matrix[j][i]);
}
}

javi
Автор

Hey, I have used recursive approach-
Time complexity: O(n^2) (Only 1 Pass)
Space complexity: Solid O(1)
Below is the implementation in Python 3.7 (Do reply if you like it):

class Solution:
def rotateImg(self, matrix, i, j, n):
if j == n-1-i:
if i == (n // 2) - 1:
return matrix
else:
return self.rotateImg(matrix, i+1, i+1, n)
matrix[i][j], matrix[n-1-j][i], matrix[n-1-i][n-1-j], matrix[j][n-1-i] = matrix[n-1-j][i], matrix[n-1-i][n-1-j], matrix[j][n-1-i], matrix[i][j]
return self.rotateImg(matrix, i, j+1, n)

def rotate(self, matrix: List[List[int]]) -> None:
dim = len(matrix)
if dim != 1:
matrix = self.rotateImg(matrix, 0, 0, dim)

aks-nckq
Автор

this is best solution for this problem I have found so far.

abhishekmore
Автор

Okay but this doesnt work for the question given no CodeStudio by Coding Ninjas

wisdomkhan
Автор

UNDERSTOOD...!!!
Thanks striver for the video... :)

ranasauravsingh
Автор

Thanks bhaiya for providing such a wonderful content.
Need your blessings to crack my dream company 🙂.

vipulrohilla
Автор

Very simple and smooth explaination really loved the explaination.

kritikarawat
Автор

we can do some more optimisation in transpose part like, we only swap when (i < j) or vice-versa

sparks
Автор

I just want to say thank you. I have been following this series for quite some time and it is helping a lot.
Your algorithmic and intuition explanations are super simple and on point and it really helps to get the concepts very easiy.
Thank you.

debjitchattopadhyay
Автор

the explanation is good enough for a beginner to understand.

AnkitKumar-vddd
Автор

Understood. Intuition very well explained. ❤️

sudesh
Автор

Absolutely correct solution in the video
/* This solution is bit incorrect */
accepted one is this
int n=m.size();

for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if(i>=j)
continue;
swap(m[i][j], m[j][i]);
}
}
for(int i=0; i<n; i++) {
reverse(m[i].begin(), m[i].end());
}

anujrawat
Автор

Hey Striver....I really love your videos....my friend is also competitive programmer but i dont understand his concepts....your concepts are best...thanks and plz try to put videos fast as there are lot of questions and placements have started

ritwickdadhich
Автор

first time i felt that i am not worst in solving problem.

OSARVEPALLILAKSHMIDEEPAK
join shbcf.ru