Amazon Coding Interview Question - 0 1 Matrix - LeetCode 542

Поділитися
Вставка
  • Опубліковано 15 гру 2024

КОМЕНТАРІ • 15

  • @danielk8452
    @danielk8452 Рік тому +1

    thx man this is the easiest one to understand out of all the ones that i've watched

  • @rachelwilliams8569
    @rachelwilliams8569 3 місяці тому +3

    One note is that this solution will add duplicate coordinates to the queue

    • @yonasbelay3343
      @yonasbelay3343 3 місяці тому

      this is what I noticed too, I am trying to figure out how you would stop it from doing so to increase efficiency

    • @dn6824
      @dn6824 3 місяці тому

      @@yonasbelay3343 just add any processed/visited coords to a set (a set of coords youve already handled) and add a check to make sure you only process a coord (aka add the coord to your queue) if and only if that coord is not in your set of coords

  • @rydog3000
    @rydog3000 Рік тому +1

    Thank you for making these videos. Keep up the good work.

    • @AlgoJS
      @AlgoJS  Рік тому +1

      No problem Ryan, will do!

  • @rachelwilliams8569
    @rachelwilliams8569 3 місяці тому

    This was the best explanation. Thank you!

  • @ethanayaay7574
    @ethanayaay7574 Рік тому +1

    Thank you for this! Your videos are a ton of help. Quick question:
    I'm having a hard time understanding the need to convert non-0's into infinity. Would it not work if you were to just compare if mat[x][y] was greater than 0?
    Also, how does it know to skip over duplicates in the queue? Like when we've already encountered an Infinity that we've already pushed to the queue?

    • @AlgoJS
      @AlgoJS  Рік тому +2

      So with setting non-0’s to infinity - it’s just a way of marking them as unvisited, then any valid distance calculated during BFS will always be less than or equal to Infinity, so it guarantees the minimum distance.
      With the duplicates - we check if the current distance to reach a cell is less than the distance already present in the matrix. If it is, we update the distance in the matrix and move to the next cell, if it isn't, then we know we haven't found an optimal path, so we skip it.
      Hope this helps!

    • @ethanayaay7574
      @ethanayaay7574 Рік тому

      @@AlgoJS Ah gotcha! I understand setting non-0's to infinity now. Although with duplicates, I'm still a bit lost.
      My understanding is that when we're looking at position [0, 1, 0], it checks all adjacent directions and pushes [1, 1, 1] into the queue since it is equal to infinity. However, when we move on and look at position [1, 0, 0], the same thing happens where we check all adjacent directions and [1, 1, 1] is pushed into the queue once more since that value is still equal to infinity. This seems to happen a third time when looking at position [1, 2, 0] resulting in [1, 1, 1] appearing in our queue three times.
      From your explanation, you mentioned that we would not push positions into the queue that we have already looked at as the queue could end up getting too large.
      I feel like I might just be missing something, so correct me if i'm wrong. Thank you again for all your help!

  • @benpurcell591
    @benpurcell591 5 місяців тому

    You explained it well.
    This question was so unintuitive for me, i was trying bfs from the 1 and of course got tle. What was the clue that makes you realise "i need to bfs from 0's, it's much more efficient"

  • @tamzidahmed9706
    @tamzidahmed9706 3 місяці тому

    you gotta show what made you decide to choose a queue.

    • @dn6824
      @dn6824 3 місяці тому

      unless u mean he should explain why we're choosing bfs then theres no reason to explain why a queue. we're trying to implement bfs which is pretty much always done with a queue. if youre unsure of why we use bfs, its bc bfs is a very handy searching algorithm when computing "shortest paths" on a graph

  • @danielk8452
    @danielk8452 Рік тому

    Btw isn't this DFS (not BFS) ?

    • @brandonfong9998
      @brandonfong9998 Рік тому +2

      BFS always uses a queue to get the values breadth-first (kind of like starting from the middle and spiraling outwards), whereas DFS uses recursion and goes all the way to the outer edge first, and makes it way back inwards