LeetCode 48. Rotate Image (Solution Explained)

Sdílet
Vložit
  • čas přidán 22. 09. 2019
  • The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
    Join my free exclusive community built to empower programmers! - www.skool.com/software-develo...
    Preparing For Your Coding Interviews? Use These Resources
    --------------------
    (My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.dev/courses/nick
    AlgoCademy - algocademy.com/?referral=nick...
    Daily Coding Interview Questions - bit.ly/3xw1Sqz
    10% Off Of The Best Web Hosting! - hostinger.com/nickwhite
    Follow My Twitter - / nicholaswwhite
    Follow My Instagram - / nickwwhite
    Other Social Media
    ----------------------------------------------
    Discord - / discord
    Twitch - / nickwhitettv
    TikTok - / nickwhitetiktok
    LinkedIn - / nicholas-w-white
    Show Support
    ------------------------------------------------------------------------------
    Patreon - / nick_white
    PayPal - paypal.me/nickwwhite?locale.x...
    Become A Member - / @nickwhite
    #coding #programming #softwareengineering
  • Věda a technologie

Komentáře • 109

  • @Xyvier
    @Xyvier Před 4 lety +17

    You are the kind of tech youtuber that I didn't know I needed.
    Thank you for doing videos like these. I have been consuming them alot lately.
    Highly informational!

  • @NuschStephany
    @NuschStephany Před 4 lety +3

    Wow! I read some articles and tried following some examples but only after your explanation, I was able to fully understand the concept and the tricks! Thank you so so much!

  • @bharatprakashparakh9601
    @bharatprakashparakh9601 Před 4 lety +6

    Brother, Superb explanation ! Earlier I thought of the same approach but got stuck in the code and your video helped me in a perfect way.

  • @xinyucao5550
    @xinyucao5550 Před 4 lety +6

    Great explanation! I had a variant of this problem yesterday in an OA, and I didn't figure out how to rotate the square matrix. wish I had watched your video earlier lol! Keep the good videos coming!

  • @luiscortes6563
    @luiscortes6563 Před 4 lety +53

    This makes much more sense than the CTCI solution explanation!

    • @AdityaKamble49
      @AdityaKamble49 Před 4 lety +5

      Exactly, Much more intuitive than that layer by layer approach

    • @faizyt12345
      @faizyt12345 Před 3 lety +3

      but im looking for MxN matrix. this will work only for a square matrix right

    • @ryebr3ad
      @ryebr3ad Před 3 lety +3

      @@faizyt12345 Correct -- it's possible to do on an NxM, though it cannot be done in place as the dimensions of the matrix will shift to MxN. The transpose code is similar, except it populates a new array instead of swaping. As an effect, it also needs to hit every element of the original matrix in order to do the transposition
      int rows = matrix.length;
      int cols = matrix[0].length;
      int rotated[][] = new int[cols][rows];
      for (int i = 0; i < cols; i++) {
      for (int j = 0; j < rows; j++) {
      rotated[i][j] = matrix[j][i];
      }
      }
      I know this reply is 7 months late; still, I hope it helped.

    • @faizyt12345
      @faizyt12345 Před 3 lety +1

      @@ryebr3ad thanks after 7 months

    • @Suraj-tu3nw
      @Suraj-tu3nw Před 3 lety

      I also came after not being able to grasp the concept from CTCI for this particular problem. 😂

  • @TheSexGod
    @TheSexGod Před 4 lety +20

    great explanation. I got stuck on this one hard until I saw this video.

  • @JustThink2000
    @JustThink2000 Před 3 lety

    of all the explanations ive seen of this problem, this explanation makes the most since.

  • @monicawang5024
    @monicawang5024 Před 4 lety +31

    I felt stuck then I watched this video. Feel alive again!!

  • @Sergey111111
    @Sergey111111 Před 2 lety

    Thanks a lot! I watched the different solution with actual rotating, but your aproach looks more sophisticated. I think the description force you to do like this showing several matrixes with swapped rows and columns

  • @baibhabmondal1740
    @baibhabmondal1740 Před 4 lety +6

    Great! I was stuck until I found a pattern and solved it recursively. But this (your method) is way simpler.
    I'll put my solution in case someone looking for a pattern.
    class Solution {
    public:
    void rotate(vector& matrix) {

    helper(matrix, matrix.size(), 0 );

    }
    void helper(vector& mat, int size, int start) {

    if(size

  • @random-0
    @random-0 Před 3 lety +1

    I was stuck on this problem from 3days and solved on my own but this approach is much better

  • @angelaferro3044
    @angelaferro3044 Před 3 lety +1

    Awesome explanation. Thank you so much for all you do!

  • @BrajBliss
    @BrajBliss Před 2 lety

    Oh god thanks for this solution. The two available on LeetCode solutions page and in some other videos are either complex or not explained properly. Finally got this into my head.

  • @julietgeorge4858
    @julietgeorge4858 Před 4 lety +1

    Another great video, Thanks, Nick!

  • @huizhao2050
    @huizhao2050 Před 3 lety

    I think that in the transpose part, you can add one condition if(i != j) because the diagonal elements don't need to be transposed.

  • @nithyasubramanian231
    @nithyasubramanian231 Před 3 lety

    Love it! Such an elegant solution

  • @ashishdukare1313
    @ashishdukare1313 Před 3 lety +4

    Thanks, man! Great Logic. It kind of reminds of Linear Algebra : )

  • @TheIndianChroniclee
    @TheIndianChroniclee Před 3 lety

    Really Really Helpful Buddy. Spent A Lot Time On It.

  • @abhinavkannojia1053
    @abhinavkannojia1053 Před 4 lety

    Best explanation by far to this problem.

  • @sithumdilanga650
    @sithumdilanga650 Před rokem

    Thank you so much. This really helped me a lot.

  • @MP-ny3ep
    @MP-ny3ep Před 4 měsíci

    Amazing explanation! Thank you very much

  • @sidhantsuman4601
    @sidhantsuman4601 Před 4 lety

    bro you are crazy man this solution is hilarious I was stuck at this for more than 4 hrs ur soln is gr8 Love from India

  • @sarthaksrivastav3408
    @sarthaksrivastav3408 Před 4 lety +1

    Excellent explanation. Thank you.

  • @pradiptahafid
    @pradiptahafid Před rokem

    this is brilliant and doable. Thanks!

  • @christopherlee3311
    @christopherlee3311 Před 3 lety

    Amazing explanation, man!! Thank you. Liked and subscribed!

  • @adarshsasidharan254
    @adarshsasidharan254 Před 3 lety +2

    I'm glad I didn't waste much time thinking about the solution on my own cuz I would have never guessed this

  • @Ben-pb7ct
    @Ben-pb7ct Před 3 lety

    Great Explanation and very clean code !

  • @Darkblaze371
    @Darkblaze371 Před 4 lety

    Made this so simple thanks!

  • @BadriBlitz
    @BadriBlitz Před 3 lety

    Good explanation.Finally i understood the approach.

  • @krishnanigalye1173
    @krishnanigalye1173 Před 4 lety

    super easy solution bro. Thanks a ton!

  • @alregith
    @alregith Před 3 lety

    Hi Nick, can we not do j=i+1 when we do the transpose?

  • @prassifromdec10
    @prassifromdec10 Před 2 lety

    the best solution so far!

  • @georgechen1124
    @georgechen1124 Před 4 lety

    excellent explanation and solution Nick! more video pls

  • @sujitha2339
    @sujitha2339 Před 2 lety

    Hi.. Nice explanation. Thanks for the video!! Could you please tell me how the time complexity is not O(N^2)?

  • @SHSelect
    @SHSelect Před rokem

    thats sick thanks Nick

  • @cracknlp364
    @cracknlp364 Před 3 lety

    You can also create a matrix and multiply to get the desired result
    i -row,j-column
    for position i=i, j=0 - multiply i-th column with [0 0 1]
    for position i=i, j=1 - multiply i-th column with [0 1 0]
    for position i=i, j=2 - multiply i-th column with [1 0 0]
    we can also think parameterizing the multiplication matrix as well. Size of the multiplication matrix is dependent on the size of the original matrix

  • @shiprashakya6106
    @shiprashakya6106 Před rokem

    Great explanation, really helped a lot. Love from India.

  • @magicundercover
    @magicundercover Před 4 lety

    Great explanation, thanks

  • @ForCodingInterview
    @ForCodingInterview Před 3 lety

    Great Explanation!!!! As always :)

  • @Chloe-si2hq
    @Chloe-si2hq Před 2 lety

    Thank you Nick!!

  • @asifnawaz3454
    @asifnawaz3454 Před 4 lety

    you made it easy.....
    Thanks

  • @DasBeatz
    @DasBeatz Před 4 lety

    Nice solution!

  • @joseeduardobarajasperez1838

    Hi Nick, thank you so much for your videos :D Why did you set j=i if technically could be j=0 but it did not work that way :c

  • @rohanmaurya4060
    @rohanmaurya4060 Před 4 lety +1

    just awesome man just awesome

  • @abdoulbarry8111
    @abdoulbarry8111 Před 2 lety

    you wanna know this if you have online assessments coming up!!! Matrix diagonals and rotation comes up alot

  • @Carlos-sy7cv
    @Carlos-sy7cv Před 4 lety

    How would you do this if it was asking for counter-clockwise? Or would you just rotate it clockwise 3 times?

    • @drukendoggy
      @drukendoggy Před 4 lety +1

      Try to transpose and do a flip on a different symmetry. See if you can figure it out, it's very similar. The same idea applies to 180 flip as well.

  • @user-kh2od5bw3u
    @user-kh2od5bw3u Před 4 lety +1

    Thanks!

  • @genghisda236
    @genghisda236 Před 4 lety

    dude don't forget to say , leave a like . I almost forgot to give a like.. you are the best.

  • @shrimatkapoor2200
    @shrimatkapoor2200 Před 3 lety

    I think you can set j to i+1 because you can skip over the same index values

  • @tarunk57
    @tarunk57 Před 3 lety

    bro thanks for everything

  • @sarvarjuraev1376
    @sarvarjuraev1376 Před měsícem

    Thanks man

  • @sakthim7160
    @sakthim7160 Před 4 lety

    Great video❤

  • @andriidanylov9453
    @andriidanylov9453 Před rokem

    Thank You

  • @shusenacademy
    @shusenacademy Před 4 lety

    Oh, man, you are awesome

  • @vighneshk509
    @vighneshk509 Před 4 lety

    great explanation only doubt how is this linear time solution if we are using 2 for loops ?? help me out please

    • @ajaypanthagani5959
      @ajaypanthagani5959 Před 4 lety

      actually he meant linear in-terms of size of entire matrix. for example if it is 3 x 3 matrix, size of matrix is 9. Here N is 9. so it takes linear time in terms of entire matrix size. think of it as a single array.

  • @daeshavvn
    @daeshavvn Před 3 lety

    Thank you

  • @KogutDV
    @KogutDV Před 3 lety

    TNX man!

  • @indranilthakur3605
    @indranilthakur3605 Před 4 lety

    Hey.. WHy do we do N/2 in the second for loop for J?

    • @aximilian15
      @aximilian15 Před 4 lety

      Is because when you are switching the elements from the first column to the last column, you stop midway through each row.

    • @indranilthakur3605
      @indranilthakur3605 Před 4 lety

      @@aximilian15 I understood that later. Thanks

  • @WowPlusWow
    @WowPlusWow Před 4 lety +2

    How is this linear time? There is 2 nested loops.

    • @raymondtan2795
      @raymondtan2795 Před 3 lety +1

      Linear with respect to the dimensions of the matrix.

  • @qwarlockz8017
    @qwarlockz8017 Před 4 lety

    I LOVE sprite rotation with a Matrix!!!!!!

    • @qwarlockz8017
      @qwarlockz8017 Před 4 lety

      Your videos really are some of the clearest and cleanest on these problems that I have seen. THANKS!

  • @shankhadeepbanerjee4980

    Thanks bro

  • @jl1835
    @jl1835 Před 3 lety

    great vid! the time complexity of this algorithm is O(N^2) not linear tho

    • @ankursao7
      @ankursao7 Před 3 lety +1

      linear for the size of matrix.

  • @adilkapadia8237
    @adilkapadia8237 Před 3 lety

    nice vid!

  • @himanshudhiman5424
    @himanshudhiman5424 Před 3 lety

    thanks bruh

  • @niraiarasu131
    @niraiarasu131 Před 4 lety +2

    what if we just reverse each row in the second loop

  • @imranimmu4714
    @imranimmu4714 Před rokem

    Thank u

  • @vk1618
    @vk1618 Před 4 lety

    Good question

  • @pretty_in_scarlet
    @pretty_in_scarlet Před 3 lety

    Same exact question in Javascript is under the "easy" category on LeetCode ?!

  • @rakshith3547
    @rakshith3547 Před 3 lety +2

    We can simplify the 2nd loop little better like
    public void rotate(int[][] matrix) {
    if (matrix == null || matrix.length

    • @pinigantikrishnavamsi7779
      @pinigantikrishnavamsi7779 Před 2 lety +1

      yes but its taking a little bit more memory but that's okay it looks more readable and understandable

  • @brrrkit
    @brrrkit Před 4 lety

    1:50 code real fine

  • @jwolfe890
    @jwolfe890 Před rokem

    cool trick.

  • @kedikebba6441
    @kedikebba6441 Před 4 lety

    Champ!

  • @rioslj9452
    @rioslj9452 Před 4 lety

    What abt an NxM ? .ex 3x5?

  • @kervensjasmin1508
    @kervensjasmin1508 Před 3 lety

    I feel less stupid. Thanks

  • @mytommy
    @mytommy Před 4 lety +1

    The time complexity for the first nested loop is O(n^2) or more accurately O(row*col), isn't it? I'm confused why you mentioned the solution is linear time complexity. Also, whats the time complexity for the second nested loop

    • @NickWhite
      @NickWhite  Před 4 lety

      mytommy listen more closely I explained it pretty clearly

    • @NickWhite
      @NickWhite  Před 4 lety +1

      Linear or O(N) where N is the number of elements in the 2d array

    • @NickWhite
      @NickWhite  Před 4 lety

      Some people say O(M*N) or O(rows*columns) which is fine as long as you specify what you’re variables are referencing

    • @mytommy
      @mytommy Před 4 lety

      @@NickWhite I know, I watched the video more than once. You mentioned the solution doesn't use data structure and the loops are separate. Still, how does that make the solution linear time complexity?

    • @nbavc
      @nbavc Před 4 lety

      ​@@mytommy Yes it is O(n^2). Outer loop is O(n) inner loop is basically n-1, n-2... which is a known series and the product of the inner and outer loop run times gets you o(n^2).

  • @flexer7822
    @flexer7822 Před 3 lety +1

    This is not a real life experience, when you open an image you get a ruster of bytes you don't a get a matrix

  • @farhan787
    @farhan787 Před 3 lety

    C++ Code to do it,
    void swapRowValues(vector& matrix, int row) {
    const int n = matrix.size();
    int left = 0;
    int right = n - 1;
    while (left < right) {
    swap(matrix[row][left++], matrix[row][right--]);
    }
    }
    void transpose(vector& matrix) {
    const int n = matrix.size();
    for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
    swap(matrix[i][j], matrix[j][i]);
    }
    }
    }
    void rotate(vector& matrix) {
    const int n = matrix.size();
    transpose(matrix);
    // Swap each row's values, e.g., col 0 with col n - 1, col 1 with col n - 2, and so on....
    for (int i = 0; i < n; i++) {
    swapRowValues(matrix, i);
    }
    }

  • @SARDARIN
    @SARDARIN Před 2 lety +1

    Your clapping of hands gave me a headache, kindly avoid that