SHORTEST DISTANCE FROM ALL BUILDING | LEETCODE 317 | PYTHON BFS SOLUTION

  Рет қаралды 7,616

Cracking FAANG

Cracking FAANG

Күн бұрын

Пікірлер: 37
@siqiliu2115
@siqiliu2115 11 ай бұрын
"I don't know who will kinda like to ask this bullsh*t" 🤣😂😂you made my day. Thank you! The best explanation for this problem!
@RamjiMisra-if6xt
@RamjiMisra-if6xt 6 ай бұрын
I was actually asked this question when I interviewed for x the moonshot factory an Alphabet company. I wasn’t able to solve it that time and was rejected. Thanks for making this video.
@moneymaker7307
@moneymaker7307 Жыл бұрын
Many people comment that this explanation is excellent but think the solution to this question is poorly explained. The key to solving the problem is the empty land. While exploring the grid, if there are buildings we cannot reach, we won't be able to mark the empty lands adjacent to unreachable buildings. So when exploring unreachable buildings, we won't find any empty land. For example, if EMPTY == -1, We won't be able to mark the unreachable buildings as -1, so their empty land ID will remain 0. That is why if there are unreachable buildings in the grid, we won't be able to find any empty land, as there will be no empty land with that id at the end. That is why the minimum distance will remain float('inf'). English is not my first language, but i hope this answers the question about the local distance and minimum distance.
@bindaddy1
@bindaddy1 5 ай бұрын
that's true, I did not understand why we need to flip the cell to -1, -2, etc until I hit an edge case in which there's no house that can reach all buildings.
@zahraaskarzadeh5618
@zahraaskarzadeh5618 2 жыл бұрын
One advantage of defining local_dist like this is that if we start from a building and we cannot reach the land then ans will stay float("inf") and we can return -1. For example grid = [[1, 1], [0, 1]], the top right building cannot reach the land and the answer should be -1.
@chiragkamat4887
@chiragkamat4887 9 ай бұрын
won't the other buildings overwrite the min_distance? coz their local_dis would be reachable.
@chiragkamat4887
@chiragkamat4887 9 ай бұрын
Found the answer for this - essentially from your example, when we kick off a bfs from top-right building, it updates the LAND variable to -2 (assuming the top-left already updated LAND=-1) without updatin grid[0][1] to -2. So any subsequent building doesn't reach that cell (even though it was a land) ever again signifying that grid[0][1] is unreachable. That is why local_dist and min_distance both remain float('inf'). In other words, the LAND variable and the "lands" in our grid should go hand in hand to indicate reachability. Hope this makes sense.
@coulsonzhang2421
@coulsonzhang2421 8 ай бұрын
Thank you for this explanation, I have the same question after watching the video and glad to discover you have put a solution here@@chiragkamat4887
@ruibinzhang
@ruibinzhang 7 ай бұрын
@@chiragkamat4887 Thank you for your explanation. The min_distance is overwrite when we start a new building.So grid[0][1] will change the min_distance to infinite number.
@secretshinki2294
@secretshinki2294 5 күн бұрын
i got asked this at meta and i almost cried...fr
@shubhamraj25
@shubhamraj25 Жыл бұрын
I just went through the matrix again to get the minimum and used the negative value to judge whether it has been visited by all the buildings or not:-> for(int i=0;i
@strawberriesandcream2863
@strawberriesandcream2863 Жыл бұрын
thank you for this video!! the concept of the solution isn't hard to grasp but it's so hard to code up in practice lol i kept writing bugs. finally was able to understand and code it up by following your solution.
@YT.Nikolay
@YT.Nikolay 2 жыл бұрын
new youtube UI, yikes... new video on the channel, poggers!!! Would you mind attaching the link to the LC problem? There is nothing extra hard about taking ID and searching it on LC, but would be faster to dig into the problem when you see a new video (yeah, it's me, I am basically taking the problem from the video and trying to crack it myself before I watched the whole video :D )
@crackfaang
@crackfaang 2 жыл бұрын
Sure I can start doing that going forward. I’ll put it in the description of the video
@KelongMao
@KelongMao 4 күн бұрын
I got this question when interviewing meta ... Damn!
@苦命醫學生
@苦命醫學生 2 жыл бұрын
Hi, a little question about local_dist, should the final ans,min_dist be update only when we get all of houses that we update the answer, right? So if each time queue is empty, we update min_dist=local_dist, won’t there be any problem?
@lostgoat
@lostgoat 2 жыл бұрын
Tried using manhattan distance than went to BFS but i think my distance checks were a bit off I was loading all homes onto the q at once and doing layers from there lol. This one is a beast thanks for sharing I will be ready for when google asks this one
@crackfaang
@crackfaang 2 жыл бұрын
Intuitively this one makes sense it’s just coding it is tricky and explaining it is even worse
@ruibinzhang
@ruibinzhang 7 ай бұрын
Thank you for your effort. There is just one point that stucks me. What about one empty room is not available from one friend's house? Should we still return the answer or -1
@ruibinzhang
@ruibinzhang 7 ай бұрын
@edwinrosemond989 Thank you. You have solved it already
@pavananand7327
@pavananand7327 Жыл бұрын
Great explanation! Also, what is the point of using min_dist if you are not doing anything with it? It's just being assigned to local_dist, you could just return local_dist
@nikenuke
@nikenuke 7 ай бұрын
The coding part makes the problem so much clearer, thank you for your effort! From the description it's so confusing on how to approach it
@crackfaang
@crackfaang 7 ай бұрын
No problem! Admittedly not one of my best videos because it's really one of the harder ones you can get and it's quite tricky to wrap your head around...even if you have the solution in front of you
@rsKayiira
@rsKayiira 2 жыл бұрын
Great explanation thank you
@crackfaang
@crackfaang 2 жыл бұрын
Thanks glad you enjoyed it. Hope the explanation made sense. This is one of the questions where it makes sense in my head but explaining it always trips me up
@rsKayiira
@rsKayiira 2 жыл бұрын
@@crackfaang Naah you killed it!!
@edwinrosemond989
@edwinrosemond989 7 ай бұрын
I may be thinking about this wrong but what if a house is isolated and can't reach any other houses. In this case min_dist is going to update to some small value, if it sees any 0, right/ How does min_dist track that that cell can reach every house?
@edwinrosemond989
@edwinrosemond989 7 ай бұрын
nevermind, the empty_land won't decrement the 0 and so proceeding 1's won't be able to see it. really cheeky!
@janeliu4430
@janeliu4430 7 ай бұрын
@@edwinrosemond989 yes! this part also tricked me. i tried to use a visited set but it didn't work
@winsortse
@winsortse Жыл бұрын
code link?
@shubhamnagota
@shubhamnagota 2 жыл бұрын
nice explanation 🙂
@crackfaang
@crackfaang 2 жыл бұрын
Thanks, glad you found it helpful. This one is quite tricky to explain
@subee128
@subee128 10 ай бұрын
Thanks
@SeanKearney-g7d
@SeanKearney-g7d 2 ай бұрын
min_dist = local_dist instead of min_dist = min(local_dist, min_dist) is confusing me
@carefree_ladka
@carefree_ladka 3 ай бұрын
It's a classic MultiSource BFS problem but I hate it .
@InAweofhisglory
@InAweofhisglory Жыл бұрын
not hard
@scuderia6272
@scuderia6272 6 ай бұрын
Very hard
EXCLUSIVE TIME OF FUNCTIONS | LEETCODE 636 | PYTHON SOLUTION
15:42
Cracking FAANG
Рет қаралды 6 М.
Smart Sigma Kid #funny #sigma
00:33
CRAZY GREAPA
Рет қаралды 7 МЛН
МЕНЯ УКУСИЛ ПАУК #shorts
00:23
Паша Осадчий
Рет қаралды 5 МЛН
Миллионер | 3 - серия
36:09
Million Show
Рет қаралды 2,1 МЛН
SHORTEST PATH IN A BINARY MATRIX | LEETCODE 1091 | PYTHON BFS SOLUTION
14:03
Being Competent With Coding Is More Fun
11:13
TheVimeagen
Рет қаралды 118 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 303 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
RANDOM PICK INDEX | LEETCODE # 398 | PYTHON RESERVOIR SAMPLING
14:56
Big-O Notation - For Coding Interviews
20:38
NeetCode
Рет қаралды 518 М.
Shortest Bridge - Leetcode 934 - Python
14:51
NeetCode
Рет қаралды 39 М.
💁‍♂️Зачем infinix это сделала?🤦‍♂️
0:42
Не шарю!
Рет қаралды 1,1 МЛН
Today's Console Pick 🔥
0:11
Gleb POV
Рет қаралды 1,4 МЛН
ИГРОВОЙ ПК от ИЛОНА МАСКА, Распаковка
32:50
How to quickly draw up a plan on iPad Procreate! #ipad手工#设计人
0:37