Rotate Matrix/Image by 90 Degrees | Brute - Optimal

  Рет қаралды 169,882

take U forward

take U forward

Жыл бұрын

Problem Link: bit.ly/3Qk14gY
Notes/C++/Java/Python codes: takeuforward.org/data-structu...
We have solved the problem, and we have gone from brute force and ended with the most optimal solution. Every approach's code has been written in the video itself. Also, we have covered the algorithm with intuition.
Full Course: bit.ly/tufA2ZYt
You can follow me across social media, all my handles are below:
Linkedin/Instagram/Telegram: linktr.ee/takeUforward
00:49 Problem statement
2:06 Observation
2:24 Brute force approach
6:25 Brute force code
7:49 Optimized approach
14:00 pseudo code for transposition
15:01 Optimized approach code

Пікірлер: 221
@takeUforward
@takeUforward 10 ай бұрын
Please watch our new video on the same topic: kzbin.info/www/bejne/kGG1Y6hsnMlmfbc
@rainyvideo6936
@rainyvideo6936 3 ай бұрын
It's a loop same video link
@rudraprasad8912
@rudraprasad8912 Жыл бұрын
The way you approach the problem.......It gives me the confidence that i can also do it!!!!!💀
@takeUforward
@takeUforward Жыл бұрын
Timestamps pleaseee. Let's march ahead, and create an unmatchable DSA course! ❤ Use the problem links in the description.
@iamvaibhav_10
@iamvaibhav_10 Жыл бұрын
notes link is not visible
@shra1
@shra1 Жыл бұрын
Love you brother, marching ahead consistently.
@aman_singh__
@aman_singh__ Жыл бұрын
TIMESTAMPS 00:49 Problem statement 2:06 Observation 2:24 Brute force approach 6:25 Brute force code 7:49 Optimized approach 14:00 pseudo code for transposition 15:01 Optimized approach code
@riyadhossain1706
@riyadhossain1706 Жыл бұрын
How you elaborate on the problem and solution is unique to any other free content I have gone through. I'll surely gonna recommend your channel if somebody asks.
@chinmay6152
@chinmay6152 Жыл бұрын
Understood. Thank you for this amazing content. I have tried many lectures but the way you approach the problem it seems extremely easy.
@alessandrocamilleri1239
@alessandrocamilleri1239 Жыл бұрын
Great explanation. I tried to come up with my solution prior to watching the video. I used the intuition of concentric squares within the matrix. I traverse one side of each concentric square and perform three swaps for each element . Since only one side of each concentric square is traversed, the number of elements traversed is approximately 1/4(m*n) and since there are 3 swaps for each element the time complexity will be O(3/4 m*n). Using a swap counter, Striver's solution is very close to O(m*n). However, unlike Striver, I do use some extra auxiliary constant space in the form of 4 pairs of co-ordinates which I use to determine the correct placing of the elements during swapping. void rotate(vector& m) { int l = 0; int h = m.size() - 1; pair a, b, c, d; while (l < h) { a = {l,l}; b = {l,h}; c = {h,h}; d = {h,l}; for (int i = l; i < h; i++) { swap (m[a.first][a.second], m[b.first][b.second]); swap (m[a.first][a.second], m[c.first][c.second]); swap (m[a.first][a.second], m[d.first][d.second]); a.second++; b.first++; c.second--; d.first--; } l++; h--; } }
@Manishgupta200
@Manishgupta200 Жыл бұрын
Best optimal explaination with in depth time complexity. Great. I do by myself with rotated by anti-clockwise and clockwise both. THankyou
@umeshkaushik710
@umeshkaushik710 Жыл бұрын
Thanks a lot bhaiya. This time I must say you are on fire. Your explaining capability is next level, bez I had problems in understanding the matrix(index and all). But Now super clear. OP Striver Guru 🔥🔥🔥🔥
@cinime
@cinime Жыл бұрын
Understood! Amazing explanation as always, thank you very much for your effort!!
@mlkgpta2869
@mlkgpta2869 11 ай бұрын
Nice explaination, the best part is that you teach how to build your mind to think in that way.....
@Krishnayadav-fu3uv
@Krishnayadav-fu3uv Жыл бұрын
very well understood, thank you for the great content ❤
@nitinpatel9259
@nitinpatel9259 10 ай бұрын
Brother you are awesome. The way you give the solutions of the problems and it very helpful for me to explain whole code(dry and run).🙏🙏
@ryanmathew6397
@ryanmathew6397 10 ай бұрын
so amazing watching your videos and getting to know how one should change there mind to observe the problem.
@mariaonokhina4493
@mariaonokhina4493 9 ай бұрын
Thank you for work you do. Really helpful!
@primespeed121
@primespeed121 10 ай бұрын
best DSA sheet ever you are the god of DSA really
@abhijeetmishra3804
@abhijeetmishra3804 9 ай бұрын
Bhaiya ur amazing . How can one explain with soo much perfection man. Live long and keep making videos for us . Hope to meet you soon .
@neilkapadia5407
@neilkapadia5407 5 ай бұрын
Understood! Amazing explanation!
@codedByAyush
@codedByAyush 22 күн бұрын
Bhaiya, apka har solutions are just too OP and easy to understand 🔥🔥
@jatinsharma1595
@jatinsharma1595 6 ай бұрын
Understood. Thank you Striver
@sayantandey4708
@sayantandey4708 10 ай бұрын
I did this optimal solution on my own, then came to see the solution video, this sheet building my confidence and skills little by little. (Rikon was my childhood friend. He worked for you some days back. No wonder why he praised you so much.)
@utsavseth6573
@utsavseth6573 Жыл бұрын
Beautifully explained.
@ManognasaiSurineniManu
@ManognasaiSurineniManu Ай бұрын
Understood Thank you for this amazing lecture sir.
@torishi82
@torishi82 2 ай бұрын
Samaj aa gaya bhai. Thank you.
@rishipandey123
@rishipandey123 6 ай бұрын
Wow sir amazing and super easy explanation ❤❤❤😊
@nikhilrajput5820
@nikhilrajput5820 10 ай бұрын
thanks you sir, i easily understand how to transpose matrix inplace.
@technicaldoubts5227
@technicaldoubts5227 Жыл бұрын
understood very well !
@satyasegu3566
@satyasegu3566 Жыл бұрын
striver so happy to learn with youuh
@harshilsutariya1793
@harshilsutariya1793 Жыл бұрын
your dedication 🙌🙌
@sarangkumarsingh7901
@sarangkumarsingh7901 3 ай бұрын
Awesome Lecture Sir.................
@konankikeerthi
@konankikeerthi Ай бұрын
Understood bro. Thank you
@_hulk748
@_hulk748 Жыл бұрын
Great Explanation❤🙇‍♂✨🙏
@tamilmukbang3789
@tamilmukbang3789 2 ай бұрын
understood. thank you so much bro
@NazeerBashaShaik
@NazeerBashaShaik 3 ай бұрын
Understood, thank you.
@samuelfrank1369
@samuelfrank1369 5 ай бұрын
Understood. Thanks a lot
@suyashshinde2971
@suyashshinde2971 Жыл бұрын
SDE Sheet Day 2 Problem 1 Done!
@rahuljmd
@rahuljmd 10 ай бұрын
Understood, thanks💚
@DesiGodOfWar
@DesiGodOfWar 3 ай бұрын
great explanation
@DeepakKumar-oz5ky
@DeepakKumar-oz5ky 7 ай бұрын
Thank u Bhaiya Very helpFul video
@HARSHA_27
@HARSHA_27 7 ай бұрын
understood!!🙇‍♂
@curs3m4rk
@curs3m4rk 3 ай бұрын
I never thought, this question was that simple :( Understood Striver, Thanks
@rajatpatra2593
@rajatpatra2593 Жыл бұрын
Understood very well
@computer_tech98
@computer_tech98 7 ай бұрын
Thank you
@RituSingh-ne1mk
@RituSingh-ne1mk 6 ай бұрын
Understood!
@infernogamer52
@infernogamer52 Жыл бұрын
Understood bhaiya!
@sayanibiswas3741
@sayanibiswas3741 2 ай бұрын
Thank you 🙏
@mind9889
@mind9889 20 сағат бұрын
I also came up with the transpose approach very happy 😁😁
@CodingEnv
@CodingEnv Жыл бұрын
I wish , I would have seen this video before makemytrip interview.. Thank you for great content.
@aryanmandi7748
@aryanmandi7748 4 ай бұрын
they ask to complete the func in interview or just write pseudo code
@mr_weird3680
@mr_weird3680 2 ай бұрын
Thank you very much brother🙇
@user-dv1ts9db8i
@user-dv1ts9db8i 3 ай бұрын
UNDERSTOOD SIR
@PankajSingh-pb4vs
@PankajSingh-pb4vs 4 ай бұрын
Understood ❤
@ddevarapaga5134
@ddevarapaga5134 11 күн бұрын
Superb UNderstood
@heyOrca2711
@heyOrca2711 3 ай бұрын
Understood! Sir
@vidushibhardwaj1415
@vidushibhardwaj1415 2 ай бұрын
thankyou so muxh for these videos❤❤... and the problem link added is a different question, but in your dsa sheet its same
@anuragprasad6116
@anuragprasad6116 5 ай бұрын
I used the idea of concentric squares to solve the problem. Say, n = 6. Now the square will be of 3X3 size. You can draw a matrix to see how the outer square is of length = 6, inside it there's a square of side = 4 and inside it there's another square of side = 2. Basically each inside square is of 2 units lesser length then its outer square. We traverse from outside to inside and rotate each square one by one. For rotation, we traverse the upper side of the square and use 3 swaps for each grid. Also, the traversal is done till 2nd last grid because if you do the dry run, you'll notice that the last grid is already swapped in the first step, i.e., the corners are common between 2 given sides. The most difficult part is to deduce the co-ordinates for the replacing element. Imagine a square which you're traversing on its top side. Now, the top left element will be replaced by bottom left, bottom left by bottom right, bottom right by top right and top right by top left. It's hard to explain in a comment how I arrived at the co-ordinates but if someone wants to try this out, instead of swapping element by element, first try swapping row by row. I've attached codes for both. Basically, store the upper side of square in a temp array then replace top row with left column, replace left column with bottom row and so on. Once you understand how that's working, the co-ordinates for element by element swap is same but using lesser extra space. If someone needs a video explanation, do reply and I'll try to post a video explaining the same. // Swapping row by row: for (int i = 0; i < n/2; i++) { vector temp; for (int j = i; j < m-i; j++) temp.push_back(mat[i][j]); for (int j = m-i-1; j >= i; j--) mat[i][j] = mat[n-1-j][i]; for (int j = m-i-1; j >= i; j--) mat[n-1-j][i] = mat[n-1-i][m-1-j]; for (int j = m-i-1; j >= i; j--) mat[n-1-i][m-1-j] = mat[j][m-1-i]; for (int j = m-i-1; j >= i; j--) mat[j][m-1-i] = temp[j-i]; } // Swapping element by element int len = mat.size(); for (int i = 0; i < len/2; i++) { for (int j = i; j < (len-i-1); j++) { int temp = mat[i][j]; mat[i][j] = mat[len-1-j][i]; mat[len-1-j][i] = mat[len-1-i][len-1-j]; mat[len-1-i][len-1-j] = mat[j][len-1-i]; mat[j][len-1-i] = temp; } }
@SatendraChauhan-ke5yr
@SatendraChauhan-ke5yr 2 ай бұрын
thankyou so much sir
@user-tk2vg5jt3l
@user-tk2vg5jt3l 5 ай бұрын
Thank you bhaiya
@AbhishekKumar-cv1dh
@AbhishekKumar-cv1dh Жыл бұрын
Understoooood 🔥😁
@shobhitsrivastava1223
@shobhitsrivastava1223 11 ай бұрын
Understood❤
@kushagramishra5638
@kushagramishra5638 Жыл бұрын
understood!
@itsabhinav04
@itsabhinav04 Жыл бұрын
Understood, thanks :)
@per.seus._
@per.seus._ 11 ай бұрын
understood❤
@rashiqajameel7822
@rashiqajameel7822 6 ай бұрын
excellent!
@aarishfaiz7880
@aarishfaiz7880 9 ай бұрын
Sir app bhut accha Padhte hoo.
@brajeshmohanty2558
@brajeshmohanty2558 Жыл бұрын
now I understood why bhai chose c++ over java because u have to write so many function in java but in c++ u have stl :( . But bro i understood the question thanku :b
@user-is6ky7pp2n
@user-is6ky7pp2n Ай бұрын
Understood !!
@nagame859
@nagame859 Жыл бұрын
Understood 👍
@anantsingh2004
@anantsingh2004 Жыл бұрын
Understood 😊
@user-ik3qu5uy5e
@user-ik3qu5uy5e 4 ай бұрын
UNDERSTOOD
@karanhaldar755
@karanhaldar755 3 ай бұрын
great job bro
@her_soulmate
@her_soulmate 10 ай бұрын
Understood 🎉
@user-uv5or5bm2c
@user-uv5or5bm2c 5 ай бұрын
Understood.
@sarthak4989
@sarthak4989 3 ай бұрын
understood ❤💘
@arjit1495
@arjit1495 Ай бұрын
Thanks. We can further improve by not using loop for reversing of row in optimal instead just use reverse after j loop is finished like this: for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { swap(mat[i][j], mat[j][i]); } reverse(mat[i].begin(), mat[i].end()); }
@nishant1456
@nishant1456 Жыл бұрын
Understand❤️
@rashi1662
@rashi1662 8 ай бұрын
this is how I wrote the transpose code for(int i=0; i< n ; i++){ for(int j=i; j < n; j++){ swap(matrix[i][j], matrix[j][i]); } } which is less confusing
@ast_karan128
@ast_karan128 Ай бұрын
bro this code is not optimal, because it this code u will traverse through all elements, and the code in the video is not traversing the diagonal element which reduce the time complexity
@rashi1662
@rashi1662 Ай бұрын
@@ast_karan128 thanks for pointing that out bro 🤜
@culeforever5408
@culeforever5408 8 ай бұрын
understood 🚴‍♂
@AzizurRahaman-zq4io
@AzizurRahaman-zq4io 8 ай бұрын
Understood
@Mohammad46552
@Mohammad46552 6 ай бұрын
understood
@DSAMADESIMPLE
@DSAMADESIMPLE Жыл бұрын
understood sir
@shaheenparween4326
@shaheenparween4326 8 ай бұрын
understood✔✔
@yamini436
@yamini436 Жыл бұрын
love u bro :)
@samitkumar18
@samitkumar18 Ай бұрын
Understand
@ghayoorhussain8930
@ghayoorhussain8930 Жыл бұрын
Java Code: ```class Solution { public void rotate(int[][] matrix) { int n = matrix.length; // Transpose of Matrix for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } // Reverse each row for (int i = 0; i < n; i++) { int left = 0, right = n - 1; while (left < right) { int temp = matrix[i][left]; matrix[i][left] = matrix[i][right]; matrix[i][right] = temp; left++; right--; } } } }```
@ishanjindal9001
@ishanjindal9001 11 ай бұрын
thanks
@khanra17
@khanra17 5 ай бұрын
9:54 We do transpose not because we need to convert rows into column. If we have a another matrix to store then we can do it directly instead of two steps. we do it so that we can swap elements. you can't do it directly. so do the extra step
@anuragroy9045
@anuragroy9045 11 ай бұрын
understood...
@ksankethkumar7223
@ksankethkumar7223 Жыл бұрын
TC for the first 2 nested for loops would be O(N*N/2) in my point of view?!
@yhbarve
@yhbarve Жыл бұрын
Hey Striver, the problem link and the video link doesn't match in the SDE sheet, please get it updated...
@rakshitrabugotra8354
@rakshitrabugotra8354 15 күн бұрын
Hi bro! I came across this problem and the first two operations on my mind were transpose and reverse. As the problem requried it to be solved in O(1) space, I carefully examined if the sequence of these operations made any signifcant changes to the performance. What I did was to reverse the matrix (row-wise) first, then take a transpose. The first operation used n/2 iterations (for optimal reversing). The second operation used n*(n+1)/2 iterations (for optimal transpose). So the total number of iterations with optimization: (n(n + 1) + n)/2 = O(n^2 + n) = O(n^2) With transpose first and then reverse each row: (n(n+1) + n^2)/2 = O(n^2 + n^2) = O(n^2) It doesn't make a difference as our PCs are blazzingly fast, but I found it neat :) Thanks a lot! This series is amazing!♥♥
@milanthakur4975
@milanthakur4975 11 ай бұрын
@roshanparajuli2172
@roshanparajuli2172 6 ай бұрын
@takeUforward, Does this works for non squared- matrix as well ??
@ayushdhiman9378
@ayushdhiman9378 Жыл бұрын
i think the time complexity of the optimal should be O(N) * O(N/2) + O(N) * O(N/2)
@thelightoffight7881
@thelightoffight7881 6 ай бұрын
How is N/2 brother i don't understand can you explain
@theornament
@theornament 5 ай бұрын
I thought that too. For the first loop, we still have to go through all of the rows except last one, which would be O(n). Now, for each row you go through, you will go through n - (i + 1) columns, which theoretically means you go through the later half of our matrix. This is why it is calculated as n/2. Then, when we loop through the rows and reverse them, we say we go through n rows and for each row we have to go loop through it to reverse them, which with the algorithm provided by Striver, it takes n/2. So, the answer would be in fact O(n * n/2) + O(n * n/2), which in simpler terms is O(n^2/2).
@HarchitGulati
@HarchitGulati 11 ай бұрын
one doubt if we have to space optimise why are we not swapping the elements in the same brute force aprroach instead of creating extra dp
@computerscience68
@computerscience68 Жыл бұрын
Please upload soon next video
@mdanashkhan5144
@mdanashkhan5144 5 ай бұрын
at 14.33vu said j loop goes till n-1 and in code at 15 .26 u have wriiten j
@abhisheksinghmehra9576
@abhisheksinghmehra9576 Жыл бұрын
Understood🔥🔥🔥
@elmo4672
@elmo4672 2 ай бұрын
trying to pass first year of college : ( thank you : )
@AkOp-bf9vm
@AkOp-bf9vm Ай бұрын
i think swap part of optimal approach take time complexity of O(N * N/2) bcz first loop is running for n times and second N/2 times
@nahidfaraji5069
@nahidfaraji5069 Жыл бұрын
Pure Platinum.
@divyanshthakur2026
@divyanshthakur2026 Жыл бұрын
Happy Ram Navami Everyone 🚩
Spiral Traversal of a Matrix | Spiral Matrix
16:33
take U forward
Рет қаралды 163 М.
Set Matrix Zeroes | O(1) Space Approach | Brute - Better - Optimal
30:07
When You Get Ran Over By A Car...
00:15
Jojo Sim
Рет қаралды 24 МЛН
Khó thế mà cũng làm được || How did the police do that? #shorts
01:00
路飞被小孩吓到了#海贼王#路飞
00:41
路飞与唐舞桐
Рет қаралды 67 МЛН
Rotate Image - Matrix - Leetcode 48
15:46
NeetCode
Рет қаралды 215 М.
Two Sum | LeetCode 1 | JavaScript | Easy
13:20
Gordon Zhu
Рет қаралды 8 М.
How to Learn Complex Skills Quickly (And Forever)
17:14
Justin Sung
Рет қаралды 17 М.
I Parsed 1 Billion Rows Of Text (It Sucked)
39:23
Theo - t3․gg
Рет қаралды 91 М.
Count Subarray sum Equals K | Brute - Better -Optimal
24:09
take U forward
Рет қаралды 231 М.
Kadane's Algorithm | Maximum Subarray Sum | Finding and Printing
20:09
take U forward
Рет қаралды 327 М.
NUMA NUMA make Raspberry Pi go ZOOMA
9:02
Jeff Geerling
Рет қаралды 66 М.
When You Get Ran Over By A Car...
00:15
Jojo Sim
Рет қаралды 24 МЛН