Rotate Image (Leetcode 48) | Full solution with examples visuals and animation | Study Algorithms

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 23. 07. 2024
  • You are given a matrix that represents an image. Each grid element can be considered as a part of a complete image. This image needs to be rotated 90 degree in a clockwise direction in a space efficient method. This video explores several ways to achieve this task along with the thought process. Based on the concept you can additionally think about flipping an image and other applications.
    Chapters:
    00:00 - Intro
    01:13 - Problem Statement and Description
    02:44 - Brute Force Method to Rotate Image
    05:03 - Concept of swapping
    06:42 - Efficient solution
    10:10 - Dry-run of Code
    13:32 - Final Thoughts
    Actual problem on LeetCode: leetcode.com/problems/rotate-...
    📚 Links to topics I talk about in the video:
    Time Complexity: ‱ What is the Time Compl...
    What is Big O?: ‱ Big O Notation Simplif...
    Brute Force Solution: ‱ Brute Force algorithms...
    LeetCode Problems: ‱ Leetcode Solutions
    Data Structures: ‱ Data Structures
    📘 A text based explanation is available at: studyalgorithms.com
    Code on Github: github.com/nikoo28/java-solut...
    Test-cases on Github: github.com/nikoo28/java-solut...
    📖 Reference Books:
    Starting Learn to Code: amzn.to/36pU0JO
    Favorite book to understand algorithms: amzn.to/39w3YLS
    Favorite book for data structures: amzn.to/3oAVBTk
    Get started for interview preparation: amzn.to/39ysbkJ
    🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
    đŸŽ„ My Recording Gear:
    Recording Light: amzn.to/3pAqh8O
    Microphone: amzn.to/2MCX7qU
    Recording Camera: amzn.to/3alg9Ky
    Tablet to sketch and draw: amzn.to/3pM6Bi4
    Surface Pen: amzn.to/3pv6tTs
    Laptop to edit videos: amzn.to/2LYpMqn
    đŸ’» Get Social đŸ’»
    Follow on Facebook at: / studyalgos
    Follow on Twitter at: / studyalgorithms
    Follow on Tumblr at: / studyalgos
    Subscribe to RSS feeds: studyalgorithms.com/feed/
    Join fan mail: eepurl.com/g9Dadv
    #leetcode #programming #rotateImage

Komentáƙe • 42

  • @madhukumar313
    @madhukumar313 Pƙed rokem +18

    I really don't know why youtube is not recommending your videos in the top list...excellenet way of explaining the problem and the solution...starting with brute force method to optimized and then dry-run of the code...best videos ever...keep the videos rolling....don't get bogged down by the view counts...

    • @nikoo28
      @nikoo28  Pƙed rokem +2

      Thank you so much for the love. I hope youtube starts showing my videos on the top soon 😄

  • @sushmas660
    @sushmas660 Pƙed rokem +3

    I don't usually leave comments. . So far, I haven't seen such a thorough explanation. thank you very much, sir

  • @justinking684
    @justinking684 Pƙed rokem +2

    I was stuck in this problem almost a day to solve without transposing, after watching this I was clear now, Thanks bro, keep uploading videos

  • @anujgupta1484
    @anujgupta1484 Pƙed 5 měsĂ­ci +1

    Bro you are truly a Gem for the coder community your explanation and teaching of each point are very understandable form.

  • @kundrapuharika6294
    @kundrapuharika6294 Pƙed 8 měsĂ­ci +2

    A very few can explain with real life examples and I am happy to find this channel which are very informative👏

  • @vishwasankar14
    @vishwasankar14 Pƙed 5 měsĂ­ci +1

    Thank you! You are the best man!

  • @eminentm1655
    @eminentm1655 Pƙed rokem +2

    much appreciated Sir, thanks.

  • @rishav-ranjan
    @rishav-ranjan Pƙed rokem +2

    this explanation helped me a lot

  • @nirajkumaryadav365
    @nirajkumaryadav365 Pƙed 5 měsĂ­ci +3

    in the second loop why you didnt use (n+1)/2 instead of n/2 ?

  • @ChaitanyaPimparkar
    @ChaitanyaPimparkar Pƙed 2 lety +1

    of immense help

  • @krrishh7
    @krrishh7 Pƙed měsĂ­cem

    As always very descriptive and neat explanation bro👍
    Regarding the loop bounds, i think it would be best the inner loop(j) runs for +1 iteration, instead of the outer loop(i).
    So conceptually, from your logical explanation, for each ring which is the ith loop, all elements in the ring get rotated as part of the j iterations, then we move to the inner ring etc.
    But if we keep i loop for more iteration(N+1/2), for odd matrixes like 3*3 say, the inner ring is just 1 element, so ideally there shouldn't be a second ring to be rotated/fixed (as its only 1 element matrix[1][1]). but we can see here that the last rotation in the outer loop is happening as part of the inner ring (i=1) iteration (which ideally should have got completed as part of i=0 itself). If we keep j range from 0 to (N+1)/2, this will get fixed. Hope that makes sense.

  • @arjunjadhav6275
    @arjunjadhav6275 Pƙed rokem +2

    thanks for these ✹

    • @nikoo28
      @nikoo28  Pƙed rokem

      glad you found them helpful

  • @sanketsuryawanshi
    @sanketsuryawanshi Pƙed 2 lety +1

    Thanks sir😇

  • @PoojaSharma0310
    @PoojaSharma0310 Pƙed rokem +1

    You solution is easy and straight forward. One question, why are you running loops (n+1)/2 and n/2? Thanks

    • @nikoo28
      @nikoo28  Pƙed rokem +1

      that is just because how we have to rotate the image. you need to visualize where the final pixels/integers will actually go. Then design your loop. So we are going one ring at a time. When you complete the first ring, you have already covered the first row and the last row. Hence the "/2".
      hope this helps.

  • @bachkhoahuynh9110
    @bachkhoahuynh9110 Pƙed 8 měsĂ­ci +1

    This solution is optimal, but it just feels too heavy to list all the indices in a cycle of swappings. So, I provide another solution using only the information that we need to carry each value at index (i, j) to the new index (j, n - 1 - i). It's easy to observe that to do a cycle of swappings, we just need to swap the first element to the second element, then the first element to the third element, ..., and finally the first element to the last element. Then, the code becomes (python3):
    int n = len(matrix);
    def nextPoint(x, y):
    return y, n - 1 - x
    for i in range((n + 1) // 2):
    for j in range(n // 2):
    x, y = nextPoint(i, j)
    while x != i or y != j:
    matrix[i][j], matrix[x][y] = matrix[x][y], matrix[i][j]
    x, y = nextPoint(x, y)
    This method works with all types of permutation.

  • @sibaathahmed1984
    @sibaathahmed1984 Pƙed 7 měsĂ­ci +1

    How u define limits as n+1/2 and n/2 respectively explain pls😱

    • @nikoo28
      @nikoo28  Pƙed 6 měsĂ­ci

      look at the part 7:10, 5 starts from 0 and reaches "n"
      If you run the loop to "n" then you will reach 5 again. We do not want that. We want to stop at middle. That is how the 4 way swap is working

  • @mehakdhiman5729
    @mehakdhiman5729 Pƙed 3 měsĂ­ci

    Can you please explain how will the elements 9,7,14,2 get swapped when we are running the inner loop only till j < n/2 which in this case would mean that j=0 or j=1. What happens to the elements that are at j=2. How will they get swapped?

    • @nikoo28
      @nikoo28  Pƙed 3 měsĂ­ci

      Watch the visual demo I have in the problem

  • @nishanthsunkara1160
    @nishanthsunkara1160 Pƙed 4 měsĂ­ci

    Could someone explain why the final elements (9, 7, 14, 2) in outer ring will get swapped, when we moved to inner ring i.e., i=1 but not when we are iterating at outer ring i.e., i=0 ?

    • @nikoo28
      @nikoo28  Pƙed 4 měsĂ­ci

      please follow the explanation instead of going straight to the code.

  • @sysybaba420
    @sysybaba420 Pƙed 9 měsĂ­ci +1

    you have not explained how you came up with all the indices, however thanks for the final thoughts!

    • @nikoo28
      @nikoo28  Pƙed 9 měsĂ­ci

      the images where I show how a rotation takes place, defines the indices

  • @shubhambarange
    @shubhambarange Pƙed 9 měsĂ­ci

    100% effect 100% Clarity

  • @xinyangli7103
    @xinyangli7103 Pƙed 3 měsĂ­ci

    i understand why n/2 because in the inner layer we only need to do half, but i dont understand why outer layer is n+1/2 , shouldn't they be the same since column and row are the same dims?

    • @nikoo28
      @nikoo28  Pƙed 3 měsĂ­ci

      Try to trace the path with the explanation I give. You will get the indices yourself.

  • @kavyakooks5305
    @kavyakooks5305 Pƙed 5 měsĂ­ci

    I think the time complexity must be O(N) , as each cell is getting read once and written once.

    • @nikoo28
      @nikoo28  Pƙed 4 měsĂ­ci

      correct, but we know that the edge size is n. So how many elements will you have? That is n^2
      Think like this, if the edge size is 5, then the area of square is 5^2 = 25.
      Since it is a cube, we have n^2 elements.

  • @franly656
    @franly656 Pƙed 9 měsĂ­ci

    hi Nikhil Lohia, i have a question, how to rotate matrix 45 degrees so the matrix size will increase, i trying to make the solution and now i want to give up (i have tried straight 5 hours btw). Can you make this solution a video pls.

    • @nikoo28
      @nikoo28  Pƙed 8 měsĂ­ci

      haven't thought about a 45 degree use case. Can you find a sample question?

  • @suneosama939
    @suneosama939 Pƙed 8 měsĂ­ci

    how to know the position likr bottom left is matrix[n - 1 - j][i]? It's hard to understand that and why above for loop (i < (n+1) / 2) and the second loop (j < n / 2), how do you know the syntax at each posititon top left top right bottom left bottom right, it's hard to understand 😱 can anybody explain how to know syntax at each posititon, omg so hard 😱â˜č

    • @nikoo28
      @nikoo28  Pƙed 7 měsĂ­ci

      Take it step by step. Did you watch the entire video? I explain the thought process.
      Where to start and then where to end.
      What part are you facing a problem with?

  • @suneosama939
    @suneosama939 Pƙed 8 měsĂ­ci +1

    Brother can you explain for me why you know the syntax concept at each position, I understand thr concept you taugh but don't know how to write it in code â˜čâ˜č how to know and write syntax at each position i wanna write 😱 so hard for me bro 😭, what is matrix[n-1-j] and matrix[n-1-i][n-j-1], matrix[j][n-1-i], and mattix[i][j] so hard brother

  • @saurabhkukreja1982
    @saurabhkukreja1982 Pƙed 7 měsĂ­ci

    Can you explain the position of indices , its confusing where to consider n-1-i Or n-j-1 . Please explain
    Otherwise best explanation of the question

    • @nikoo28
      @nikoo28  Pƙed 6 měsĂ­ci

      walk through the explanation step by step. See how you are starting from outside and then going in 1 row/column at a time. Trace out and try printing the elements. That will explain everything.

  • @mayankpathak6827
    @mayankpathak6827 Pƙed měsĂ­cem

    sir you are doing awesome but your voice of video is very low

    • @nikoo28
      @nikoo28  Pƙed 22 dny

      i have fixed that in my latest videos. Check them out and please let me know

  • @omsudhamsh.h
    @omsudhamsh.h Pƙed měsĂ­cem

    ok simple
    firstly Transpose the given matrix and then reverse it.
    -------------------------------------------------------
    Runtime
    0
    ms
    Beats
    100.00%
    of users with Java