SHORTEST PATH IN A BINARY MATRIX | LEETCODE 1091 | PYTHON BFS SOLUTION

Sdílet
Vložit

Komentáře • 25

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

    Great explanation. Thanks! Definitely subscribing.

  • @Jessica-gu6rn
    @Jessica-gu6rn Před dnem

    Great video. If the output is to print out the shortest path with all nodes in it, how would you solve it? thanks!

  • @jeremypruitt237
    @jeremypruitt237 Před 2 lety

    Great explanation! Thanks much!!

    • @crackfaang
      @crackfaang  Před 2 lety

      Thanks for the kind words! Make sure to subscribe so you don’t miss future videos and let me know if there’s any problems you’d like me to solve

  • @akhileshshahare
    @akhileshshahare Před 2 lety

    Clear explanation. Thanks!

    • @crackfaang
      @crackfaang  Před 2 lety

      Thanks for your support! Make sure you subscribe so you don’t miss future uploads

  • @avivsmadja9159
    @avivsmadja9159 Před rokem +1

    How Can you do the same but mark the Path and not only return what length it is

  • @jedholtman9075
    @jedholtman9075 Před 2 lety

    A corollary is that the range returned (assuming a path exists) is n

  • @rachnaramkumar
    @rachnaramkumar Před 2 lety +2

    Thanks for another great video :) Can you solve Leetcode 31. Next Permutation? I don't see many videos for it. I do see a couple of solutions posted in the discuss section, but I can't wrap my head around it. Thanks :)

    • @crackfaang
      @crackfaang  Před 2 lety +1

      Haha sure. By the way I absolutely hate Next Permutation. It’s a question I can write a solution for but I have no idea how the proof that the solution is valid works 😂

    • @rachnaramkumar
      @rachnaramkumar Před 2 lety +1

      @@crackfaang I get what you mean. I got the solution, but I don't exactly know why it's the solution :P

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

    Shouldn't it be N*N time complexity?

  • @thedropshipper96
    @thedropshipper96 Před rokem +3

    in this solution how it is ensuring its the shortest path it just going random path and there is no conditions that tells its the shortest path. Can you please explain this?

    • @crackfaang
      @crackfaang  Před rokem +3

      You can think of the matrix and the paths as an undirected, unweighted graph. The BFS algorithm is guaranteed to give the shortest path when traversing through such a graph.
      You can think of this intuitively as BFS will explore all paths of length N, before moving onto paths of length N + 1. Also we can sanity check this by the fact that the solution is accepted. The chances of us going down a “random” path and passing all test cases every time we submit is essentially impossible

    • @pravinpoudel1307
      @pravinpoudel1307 Před rokem +1

      @@crackfaang this is a valid question, i think your solution works because you in your direction, you are putting diagonal direction at last

    • @pravinpoudel1307
      @pravinpoudel1307 Před rokem

      @@crackfaang i am sorry i was wrong, it does matter because the distance is height of the tree or number of level you had to trace and whenever you get the answer, it mean that is the fastest !!! unlike in DFS, in BFS first one to meet the base condition is the shortest or fastest

  • @SaurabhSingh-vv7wt
    @SaurabhSingh-vv7wt Před 2 lety

    great job man , but can u provide the o(n) solution

    • @crackfaang
      @crackfaang  Před 2 lety

      This is O(N) where N is the number of cells in the grid. I may have said O(rows * cols) but that's the same thing in the end. It's linear in that it depends on the size of the grid

  • @_7__716
    @_7__716 Před 2 lety

    Thanks. Please zoom a bit for mobile users.

    • @crackfaang
      @crackfaang  Před 2 lety +3

      Thanks for the feedback. Unfortunately almost all of my audience is desktop users and if I zoom in, then you won’t be able to see the full solution in code and will have to scrub the video to read it which I find is a very bad UX

    • @crackfaang
      @crackfaang  Před 2 lety +3

      I am currently on vacation in the Bahamas but when I get back I will try to make a GitHub repo with all the solutions for you

  • @GeraudKAMENI
    @GeraudKAMENI Před 4 měsíci

    time complexity is the size of matrix N*N

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

    Didn't get why it was the shortest one?

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

      Breath First Search in an un-directed graph is always guaranteed to be the shortest path. There's probably a proof out there somewhere but this is just a general thing you learn and apply for these categories of problems.