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

  Рет қаралды 14,895

Cracking FAANG

Cracking FAANG

Күн бұрын

Пікірлер: 27
@jedholtman9075
@jedholtman9075 2 жыл бұрын
A corollary is that the range returned (assuming a path exists) is n
@thedropshipper96
@thedropshipper96 2 жыл бұрын
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 2 жыл бұрын
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 Жыл бұрын
@@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 Жыл бұрын
@@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
@Jessica-gu6rn
@Jessica-gu6rn 4 ай бұрын
Great video. If the output is to print out the shortest path with all nodes in it, how would you solve it? thanks!
@suraj8092
@suraj8092 9 ай бұрын
Shouldn't it be N*N time complexity?
@avivsmadja9159
@avivsmadja9159 2 жыл бұрын
How Can you do the same but mark the Path and not only return what length it is
@rachnaramkumar
@rachnaramkumar 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 2 жыл бұрын
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 2 жыл бұрын
@@crackfaang I get what you mean. I got the solution, but I don't exactly know why it's the solution :P
@GeraudKAMENI
@GeraudKAMENI 9 ай бұрын
time complexity is the size of matrix N*N
@akhileshshahare
@akhileshshahare 2 жыл бұрын
Clear explanation. Thanks!
@crackfaang
@crackfaang 2 жыл бұрын
Thanks for your support! Make sure you subscribe so you don’t miss future uploads
@jeremypruitt237
@jeremypruitt237 2 жыл бұрын
Great explanation! Thanks much!!
@crackfaang
@crackfaang 2 жыл бұрын
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
@muskanmall4401
@muskanmall4401 19 сағат бұрын
thanks man!
@ashleypowell8066
@ashleypowell8066 Жыл бұрын
Great explanation. Thanks! Definitely subscribing.
@SaurabhSingh-vv7wt
@SaurabhSingh-vv7wt 2 жыл бұрын
great job man , but can u provide the o(n) solution
@crackfaang
@crackfaang 2 жыл бұрын
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
@sarayarmohammadi3376
@sarayarmohammadi3376 Ай бұрын
Thank you very much!
@_7__716
@_7__716 2 жыл бұрын
Thanks. Please zoom a bit for mobile users.
@crackfaang
@crackfaang 2 жыл бұрын
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 2 жыл бұрын
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
@romanivanov6183
@romanivanov6183 6 ай бұрын
Didn't get why it was the shortest one?
@crackfaang
@crackfaang 6 ай бұрын
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.
Shortest Path in a Binary Matrix - Leetcode 1091 - Python
12:34
NeetCodeIO
Рет қаралды 27 М.
БУ, ИСПУГАЛСЯ?? #shorts
00:22
Паша Осадчий
Рет қаралды 3 МЛН
Noodles Eating Challenge, So Magical! So Much Fun#Funnyfamily #Partygames #Funny
00:33
Миллионер | 3 - серия
36:09
Million Show
Рет қаралды 2,1 МЛН
Why no RONALDO?! 🤔⚽️
00:28
Celine Dept
Рет қаралды 84 МЛН
Breadth First Search grid shortest path | Graph Theory
16:51
WilliamFiset
Рет қаралды 339 М.
G-36. Shortest Distance in a Binary Maze
23:42
take U forward
Рет қаралды 156 М.
LOWEST COMMON ANCESTOR OF A BINARY TREE III [PYTHON]
16:38
Cracking FAANG
Рет қаралды 12 М.
Breadth First Search Algorithm | Shortest Path | Graph Theory
7:23
WilliamFiset
Рет қаралды 723 М.
MAKING A LARGE ISLAND | LEETCODE # 827 | PYTHON SOLUTION
22:09
Cracking FAANG
Рет қаралды 8 М.
Shortest Path in Binary Matrix | Live Coding with Explanation | Leetcode - 1091
10:23
ACCOUNTS MERGE | LEETCODE # 721 | PYTHON SOLUTION
23:04
Cracking FAANG
Рет қаралды 12 М.
БУ, ИСПУГАЛСЯ?? #shorts
00:22
Паша Осадчий
Рет қаралды 3 МЛН