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

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

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.
@secretshinki2294
@secretshinki2294 Күн бұрын
i got asked this at meta and i almost cried...fr
@zahraaskarzadeh5618
@zahraaskarzadeh5618 Жыл бұрын
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 6 ай бұрын
@@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.
@KelongMao
@KelongMao 19 сағат бұрын
I got this question when interviewing meta ... Damn!
@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.
@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
@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
@苦命醫學生
@苦命醫學生 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?
@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
@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 6 ай бұрын
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 6 ай бұрын
@edwinrosemond989 Thank you. You have solved it already
@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!!
@subee128
@subee128 9 ай бұрын
Thanks
@shubhamnagota
@shubhamnagota 2 жыл бұрын
nice explanation 🙂
@crackfaang
@crackfaang 2 жыл бұрын
Thanks, glad you found it helpful. This one is quite tricky to explain
@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 6 ай бұрын
@@edwinrosemond989 yes! this part also tricked me. i tried to use a visited set but it didn't work
@winsortse
@winsortse Жыл бұрын
code link?
@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 М.
MAKING A LARGE ISLAND | LEETCODE # 827 | PYTHON SOLUTION
22:09
Cracking FAANG
Рет қаралды 8 М.
They Chose Kindness Over Abuse in Their Team #shorts
00:20
I migliori trucchetti di Fabiosa
Рет қаралды 12 МЛН
風船をキャッチしろ!🎈 Balloon catch Challenges
00:57
はじめしゃちょー(hajime)
Рет қаралды 92 МЛН
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 9 МЛН
Ice Cream or Surprise Trip Around the World?
00:31
Hungry FAM
Рет қаралды 21 МЛН
SHORTEST PATH IN A BINARY MATRIX | LEETCODE 1091 | PYTHON BFS SOLUTION
14:03
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 197 М.
EXPRESSION ADD OPERATORS | LEETCODE # 282 | PYTHON SOLUTION
24:05
Cracking FAANG
Рет қаралды 7 М.
MERGE K SORTED LISTS | LEETCODE 23 | PYTHON OPTIMAL SOLUTIO
19:33
Cracking FAANG
Рет қаралды 3,3 М.
Bresenham's Line Algorithm - Demystified Step by Step
16:10
NoBS Code
Рет қаралды 61 М.
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 442 М.
ACCOUNTS MERGE | LEETCODE # 721 | PYTHON SOLUTION
23:04
Cracking FAANG
Рет қаралды 11 М.
Faster than Rust and C++: the PERFECT hash table
33:52
strager
Рет қаралды 607 М.
BEST MEETING POINT | LEETCODE 296 | PYTHON OPTIMAL SOLUTION
18:06
Cracking FAANG
Рет қаралды 2,4 М.
They Chose Kindness Over Abuse in Their Team #shorts
00:20
I migliori trucchetti di Fabiosa
Рет қаралды 12 МЛН