DIAGONAL TRAVERSE | LEETCODE # 498 | PYTHON SOLUTION

Sdílet
Vložit
  • čas přidán 20. 05. 2022
  • In this video we are solving another Facebook interview question: Diagonal Traverse (Leetcode # 498).
    This problem is really easy to understand and think through conceptually, but there are a few annoying edge cases we need to worry about when writing the code that make this problem slightly tricky. But don't worry we'll go over how to solve this and you'll understand it in no time!
  • Věda a technologie

Komentáře • 26

  • @matthewsaucedo2471
    @matthewsaucedo2471 Před 8 měsíci +6

    Always know that I'll be able to understand a problem when I see you've made a video on it. Thanks a lot man, this was great!

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

    Another way to do it is consider that the elements in the same diagonal list will share (i+j) value, and the order will alternate each diagonal in order ascending (i+j). A defaultdict(list) makes it pretty easy.
    Nonetheless, thanks for sharing your approach and all your videos in general. Super helpful!

  • @mindrust203
    @mindrust203 Před 3 měsíci +3

    Time and space complexity should be O(M*N) shouldn't it? We are processing all elements in an M*N matrix

  • @kevinnguyen4222
    @kevinnguyen4222 Před rokem +4

    Hey, thank you a lot for this solution!
    If anyone is interested in another solution I thought I'd share mine.
    You can realize that there at most m + n - 1 diagonals (in the first example there are 2 rows and 2 cols so there are 4 diagonals). For every cell, we can get its "diagonal index" (0-based) by adding the current row and column of that cell. For example, 5 in the first example, it is in row 1 and column 1, and so it has a diagonal-index of 2 (again, 0-based).
    Knowing these facts, you can initialize a list of lists and traverse the matrix, adding each cell into its corresponding diagonals. At the end though, you'd find that even-numbered diagonals are reversed, so you have to reverse them. Another annoying thing you have to do is you have to flatten the list of lists into 1 big list.
    Not sure if that made sense but it was quite intuitive for me. It was also only about 8 lines of code and ran pretty quickly.

    • @aman4434
      @aman4434 Před 5 měsíci

      great approach, good idea to keep in mind for diagonal related questions (i remember doing N Queens with this property!)

  • @readonlylogin
    @readonlylogin Před rokem +1

    Your solution is easier to understand than official one!

  • @melody-eu4ko
    @melody-eu4ko Před 5 měsíci +2

    Thank you soo much ...i gone through many vedios on this particular question but none of them helped me finally urs is amazing and very easy to understand for the beginners like me 😊😊

    • @crackfaang
      @crackfaang  Před 5 měsíci

      Hey no problem! Glad you are getting value out of the content and make sure to subscribe if you haven't already

  • @sayeedchowdhury11
    @sayeedchowdhury11 Před 3 měsíci

    Excellent videos! Really appreciate it

  • @sharwariphadnis1298
    @sharwariphadnis1298 Před 2 měsíci

    Thanks for the nice explanation!

  • @JatasanVietnamese
    @JatasanVietnamese Před 5 měsíci

    Thank you for the simple and very easy to understand solution! Also your voice is so soothing to listen to lol

    • @crackfaang
      @crackfaang  Před 5 měsíci +1

      No problem mate, glad you enjoyed the video and found it useful

  • @seanthomas8907
    @seanthomas8907 Před 11 měsíci

    Thank you very much! You made it super easy to understand

    • @crackfaang
      @crackfaang  Před 11 měsíci

      No problem, glad you enjoyed the video

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

    I just traversed the same over the matrix, but every 2nd iteration i've flipped the diagonal

  • @teodorchirileanu
    @teodorchirileanu Před 2 lety

    thanks

  • @subee128
    @subee128 Před 5 měsíci

    Thanks

  • @vasujhawar.6987
    @vasujhawar.6987 Před rokem

    cool!

  • @SS-cz3vg
    @SS-cz3vg Před 2 lety

    I really like this solution.

    • @crackfaang
      @crackfaang  Před 2 lety

      Thanks glad you found the video informative. Make sure to subscribe so you don’t miss future uploads

  • @rsKayiira
    @rsKayiira Před rokem

    Thank you could you do Diagonal Traverse II

  • @madikhan-only
    @madikhan-only Před 2 lety

    You are best

    • @crackfaang
      @crackfaang  Před 2 lety

      Thanks for watching and commenting! Really helps out

  • @asrahussain8642
    @asrahussain8642 Před 9 měsíci

    change ur name its cringe

    • @crackfaang
      @crackfaang  Před 9 měsíci +6

      Thank you random stranger on the internet. Whatever would I do without your sage like wisdom and guidance. You know I was really lost in this world until I read this comment but now I have purpose and meaning again.Who would have thought that such enlightenment would come from someone who spells "your" as "ur" and does not know to use an apostrophe in the word "it's". It's truly fascinating how the universe works sometimes

    • @user-hd4sl
      @user-hd4sl Před 9 měsíci

      @@crackfaang😂😂😂😂