DIAGONAL TRAVERSE | LEETCODE # 498 | PYTHON SOLUTION
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
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!
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!
Time and space complexity should be O(M*N) shouldn't it? We are processing all elements in an M*N matrix
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.
great approach, good idea to keep in mind for diagonal related questions (i remember doing N Queens with this property!)
Your solution is easier to understand than official one!
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 😊😊
Hey no problem! Glad you are getting value out of the content and make sure to subscribe if you haven't already
Excellent videos! Really appreciate it
Thanks for the nice explanation!
Thank you for the simple and very easy to understand solution! Also your voice is so soothing to listen to lol
No problem mate, glad you enjoyed the video and found it useful
Thank you very much! You made it super easy to understand
No problem, glad you enjoyed the video
I just traversed the same over the matrix, but every 2nd iteration i've flipped the diagonal
thanks
Thanks
cool!
I really like this solution.
Thanks glad you found the video informative. Make sure to subscribe so you don’t miss future uploads
Thank you could you do Diagonal Traverse II
You are best
Thanks for watching and commenting! Really helps out
change ur name its cringe
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
@@crackfaang😂😂😂😂