Leetcode 542. 01 Matrix - Python

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

КОМЕНТАРІ • 18

  • @peternguyen9995
    @peternguyen9995 2 роки тому +2

    Not only do you explained it quite well, but your voice is also quite soothing, which makes the lesson enjoyable.

  • @prachisaxena1600
    @prachisaxena1600 3 роки тому +8

    Great tutorial. Thanks for putting in time to help others.

  • @ahmadahmed6746
    @ahmadahmed6746 3 роки тому +6

    Great Tutorial Man!

  • @Zephromonia
    @Zephromonia 2 роки тому +1

    Thanks for the tutorial! This helped me understand.

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

    how does this code guarantee the shortest distance?

    • @clancy2570
      @clancy2570 9 місяців тому +3

      I was having a hard time understanding this at first, too. Basically, because in the second half our q at first only holds the coordinates of where we have 0s, so we find the #s that are next to the 0s, and thus one space away. Once we do this, all the spaces that are 1 space away from a zero now contain 1 and are added to our q. The for loop continues through and looks at the coordinates that now contain 1s and looks for any #s that may be next to them, and so on.
      Because we are starting at the 0s and working our way up within the q we will always find the shortest distance as the further ones will not be seen when we look up,down,left,right. I'm sure there's a better way to say it, but hopefully this can help someone a little.

  • @johnmauricio8490
    @johnmauricio8490 2 роки тому +11

    Hey man great video but you should focus more on the solution as opposed to explaining what parts of the code do syntatically. Anyone who is attempting leetcode mediums should already understand things like accessing an element in a 2D array.

  • @chanyaoc
    @chanyaoc 2 роки тому +4

    Thank you for the explanation. While this is algorithmically correct, it may fail in large test case. Specifically, if the distance of a cell is previously computed as 35 (exactly the ascii value of ‘#’), your code will mistakenly flag it as not being computed and therefore incrementing it again (and potentially more than once due to the same reason). I suggest you change ‘#’ to either some negative number or maximum of int so this corner case will be fine. Otherwise, love the short and concise explanation!

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

    very clear explanation, thanks

  • @aalemsingh9658
    @aalemsingh9658 3 роки тому +1

    Just a quick question - If you assign the value of mat[ i ] [ j ] to " # " , how are you able to add 1 to it in the bottom if statement. Wouldn't it be 1 + "#"

    • @vudat1710
      @vudat1710 3 роки тому

      no it wont. You can see that it will only update coordinates if previously traversed coord was visited and its value was updated (from 0)

    • @DerekGomez-n1w
      @DerekGomez-n1w 18 днів тому

      mat[ i ][ j ] always equals an int. mat[ i+dx ][ j+dy ] always equals "#" in the conditional

  • @thermalmedia6454
    @thermalmedia6454 3 роки тому +1

    Nice explanation. Would it be better to solve it recursively or not?

  • @dulatamirkhanov8271
    @dulatamirkhanov8271 2 роки тому

    easy understanding)