Technical Interview Question: Number of Islands [LeetCode]

  Рет қаралды 32,234

AlgosWithMichael

AlgosWithMichael

Күн бұрын

Пікірлер: 142
@pogostemoncablin4507
@pogostemoncablin4507 2 жыл бұрын
I can't thank you enough, you are a great teacher. I am learning graphs from you and I'm getting over my fear of graphs because of you.
@usmanh770
@usmanh770 4 жыл бұрын
I just went through all the big youtubers who explained this, and you explained it the best, yet you have the least subscribers - please, I urge you to keep going, you'll gain traction soon, really great quality stuff, good job!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you so much, I really appreciate that!
@asgm1382
@asgm1382 2 жыл бұрын
I second this.
@MohammadSalman-zq2rv
@MohammadSalman-zq2rv 2 жыл бұрын
By far the best mentor
@koredyte
@koredyte 4 жыл бұрын
Best video on this topic. I particularly like the 'sink' analogy, and also the fact you took the time to explain the problem in detail first!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you so much, more videos to come!
@robertsedgewick1266
@robertsedgewick1266 3 жыл бұрын
Many explanations on youtube for this question, but this is by far the clearest! The specific naming of changeLandToWater( ) makes it so much clearer compared to dfs( )
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thank you very much!
@panzabamboo1901
@panzabamboo1901 5 жыл бұрын
Best vid I have seen on this, thanks!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
thank you
@majetyprashanth1590
@majetyprashanth1590 3 жыл бұрын
Never understood this problem's solution this clearly ! You've explained every single small step in detail .. I'm really glad to have found your channel ! Keep going !!! ^_^ At the end, I was like this problem is so easy and I could not believe that the solution was so easy using recursion. All thanks to you brother :)
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Awesome to hear! Thanks for watching
@dimejifaluyi1759
@dimejifaluyi1759 6 жыл бұрын
Really great explanation. By 4:31 I knew where you were going and had that 'Ah ha' moment and was able to implement it from there on. Thanks!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
nice, thanks for watching
@Dragondavid
@Dragondavid 6 жыл бұрын
Man you finally made me to understand recursion!!!!!!!! my hero!!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
that is exactly what i wanted :)
@lylez00
@lylez00 Ай бұрын
I've been interviewing lately, and I'm not encountering any of these "common questions" I'm seeing on KZbin. They're all different, and they're all hard. My question today - just the question and the examples of solutions was 63 lines, and I had about 20 minutes to solve it. I am so sick of the IT industry!
@2412Anand
@2412Anand Жыл бұрын
Hey Michael, Thank you for this amazing playlist. As I see the maximum questions in the playlist is consist of DFS. Can you make some set of questions where we need to apply BFS and show us how to apply?
@Ryan-fe2du
@Ryan-fe2du 5 жыл бұрын
Best explanation I have seen. Thanks!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you, I appreciate the kind words!
@90krishika
@90krishika 5 жыл бұрын
Best explanation on this topic! Loved it (y)
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
glad to hear!
@anpascally
@anpascally 5 жыл бұрын
Great video, great explanation. Thank you so much.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
of course, thank you for watching
@soumilmishracomp
@soumilmishracomp 4 жыл бұрын
The video is really helpful. Keep up the good work and I request you to please draft some more similar videos for other leetcode problems.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Most definitely! Thank you for watching!
@shihaojin5338
@shihaojin5338 4 жыл бұрын
very clear and helpful. Thanks
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
No problem, thanks for watching! Check out some of my newer graph related videos if you like this one, I've tried to improve the quality of the videos!
@tetianao376
@tetianao376 6 жыл бұрын
such a great explanation!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
awesome!
@RedionXhepa
@RedionXhepa 3 жыл бұрын
Really nice content !
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thanks!
@johnkim7783
@johnkim7783 5 жыл бұрын
This is a really good explanation! Great job doing in !
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
anytime :)
@snigdhapurohit4216
@snigdhapurohit4216 6 жыл бұрын
Thank you so much for explaining this problem so well. :)
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
anytime
@DanielVazquez
@DanielVazquez 3 жыл бұрын
Good explanation, just a tiny detail: you just missed to change the firsts "ones" occurrences to zeros in the graphic explanation.
@kinjalthehero
@kinjalthehero 5 жыл бұрын
Thank you for the clear explanation.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
No problem!
@pavanomanwar1389
@pavanomanwar1389 6 жыл бұрын
Very easy and clear explanation!!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
awesome to hear
@KazuhiroOinuma
@KazuhiroOinuma 5 жыл бұрын
So easy to understand!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Glad to hear :)
@dustinv9193
@dustinv9193 5 жыл бұрын
Great video. Thanks for your clear explanation.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
of course!
@ravikumarnarayanan5876
@ravikumarnarayanan5876 6 жыл бұрын
Awesome, just made me understand this in one shot.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
glad to hear
@jeffge8009
@jeffge8009 5 жыл бұрын
very clear explanation! thanks
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
awesome, thanks for watching
@ShashwataSaha
@ShashwataSaha 3 жыл бұрын
For the same algo it's saying maxing recursion depth excided, In pythn
@KnowledgeAmplifier1
@KnowledgeAmplifier1 5 жыл бұрын
Very nice explanation ... Clear idea:-)
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Nice, thanks for watching!
@jingjing6813
@jingjing6813 5 жыл бұрын
very clear explanation!!!! Thanks for sharing!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
no problem
@lylez00
@lylez00 2 ай бұрын
This solution modifies the input data. To avoid this, you could create a set of points and check for the presence of a point (make sure your point class overrides both hashcode and equals) in that set, or copy the input two-dimensional array.
@mohammadn0man
@mohammadn0man 3 жыл бұрын
great explanation
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thank you!
@subhadeepbhattacharyya2867
@subhadeepbhattacharyya2867 6 жыл бұрын
I am not sure if your up and down notations are correct.
@anpascally
@anpascally 5 жыл бұрын
Yeap. Up should be 'i-1' and Down should be 'i+1'
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Yea, I messed them up lol
@woodylucas
@woodylucas 3 жыл бұрын
Every time you ever go over a problem I say to myself "Why did I ever struggle?"
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
haha I'm glad the vids are helping!
@khangt1k25
@khangt1k25 5 жыл бұрын
Best video, it's really useful. Thanks!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
i appreciate that
@SumitKumar-ww7he
@SumitKumar-ww7he 4 жыл бұрын
Excellent.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
thank you
@vincenr8822
@vincenr8822 6 жыл бұрын
Nice! You should create more videos like this but with other problems. 👍🏻
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Only 2 years late, but I have been uploading every week now :p
@shameedify
@shameedify 3 жыл бұрын
Up and Left recursive calls are not needed the way you are iterating in the caller.
@karthikmoh
@karthikmoh 6 жыл бұрын
Great explanation
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you!
@ramireddy8223
@ramireddy8223 6 жыл бұрын
Really great explanation ...
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
i appreciate that
@SameerSrinivas
@SameerSrinivas 5 жыл бұрын
Great explanation! Could you please tell what is the run time complexity? Both space and time?
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Both the time and space will be O(M*N). Sorry for the very late reply lol
@MrThepratik
@MrThepratik 5 жыл бұрын
Awsme video Thanks a ton
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
anytime
@qaipak1
@qaipak1 5 жыл бұрын
some home slices be doing this with dem trees dough
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
fuck yea
@udaychatterjee4424
@udaychatterjee4424 3 жыл бұрын
Do we need to call the recursive function for up and left? I think calling it for down and right shoulder care of it
@ForCodingInterview
@ForCodingInterview 3 жыл бұрын
Yes.. Consider the below input [["1","1","1"],["0","1","0"],["1","1","1"]] If top & left are not called, it will give 2 as output. But expected output is 1
@udaychatterjee4424
@udaychatterjee4424 3 жыл бұрын
@@ForCodingInterview ahhh. Make sense. Thanks for explaining.
@danielzuzevich4161
@danielzuzevich4161 3 жыл бұрын
Thank you.
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
No worries!
@ArghyaBhattacharyaNITA
@ArghyaBhattacharyaNITA 3 жыл бұрын
what if there is 1 in the top row 3rd place from the left.. as per this algorithm it will count that as an island, but that would be wrong right..
@DanielVazquez
@DanielVazquez 3 жыл бұрын
When we convert land (1) into water (0) in the recursive method, we make sure to visit every horizontal and vertical neighbor from every land cell. So, if we start at the top left position (0,0) which contains a "1" we convert it to water "0" and we immediately visit the right (0,1) and bottom (1,0) cells recursively. First, we we visit (0,1) which is the top row 2nd place and we do the same: we mark it as water "0" and we visit the horizontal and vertical cells containing land, thus this reaches the right cell (0,2) which is the top 3rd place from the left, so it will be counted as part of the first island. I hope this helps.
@DanielVazquez
@DanielVazquez 3 жыл бұрын
Is there any advantage of solving this problem using DFS vs BFS?
@MrMayurB
@MrMayurB 5 жыл бұрын
Need help with Most Stones Removed with Same Row or Column - [Leetcode], it is similar problem but could not understand.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
im going to do a video on that problem
@nagesh007
@nagesh007 2 ай бұрын
Super
@saulgoodman6710
@saulgoodman6710 Жыл бұрын
This solution shouldnt work since you're not considering diagonal cells which are also part of the island right?
@Srikanthking1
@Srikanthking1 5 жыл бұрын
Can you explain open the lock problem of leetcode? Also can this be done using bfs instead of dfs? I tried bfs and got timeout exception in leetcode
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
ill have to look at this problem, i havent seen it before. thanks for watching!
@marceldegas
@marceldegas 6 жыл бұрын
When I try this implementation in Ruby, I get "stack too deep" error
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
2 years late lol, but I would say it is because your exit condition is not correct
@india1727
@india1727 6 жыл бұрын
If the case is to find the number of connected islands we will be using the diagonals along with the top and downs, am i right?
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
for this problem, an island does not include diagonals
@milaness2730
@milaness2730 6 жыл бұрын
Very good
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
thank you
@saswatibhattacharjee7387
@saswatibhattacharjee7387 5 жыл бұрын
Great explanation. But when I submit this code in Leetcode it throw a error message Java.lang.stackoverflow error. Can you help me?
@rico5146
@rico5146 5 жыл бұрын
This solution is dfs in matrix so i think that you maybe didn't check the duplicates. if dfs is proceeding and next node not checked as "visited" then it will result in stack overflow error exception. You can handle it simply by making 2d array for visited check. once your dfs function visited next node or cell(in this matter)you should do "visited check for that node" to prevent the next dfs from making duplicate visits. Make a cheke array such as visited[r][c] and when visited a node location at x and y, then assign integer 1 in visited[x][y]. Hope this help.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
a year late lol... but stackoverflow error is usually a problem with your base conditions in your dfs function
@pranupranup8285
@pranupranup8285 5 жыл бұрын
in your example you have 5*5 , but in the leet code problem, its 4*4 , but how did you get the count as 3??
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
regardless of the size of the array, the number of islands was the same
@MrPrakash219
@MrPrakash219 6 жыл бұрын
Good one.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
thank you
@yafeiwang730
@yafeiwang730 5 жыл бұрын
Is this BFS method? Thank you!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
No, this is the DFS method.
@sifou7974
@sifou7974 5 жыл бұрын
thanks
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Of course!
@monikajha3500
@monikajha3500 6 жыл бұрын
Is column length always grid[0].length ?? I couldnt understand line 7. or should it be grid[i].length? Please help
@niksgupta36
@niksgupta36 5 жыл бұрын
grid[0].length is gonna be always same as grid[i].length, so it does not matter. Put anything!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
since they are the same length of row and column, it wont matter
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
yep, thanks for explaining
@rajathanand93
@rajathanand93 5 жыл бұрын
Can you solve K closest points to the Origin? Leetcode - 973
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Yep, that problem is top on the list.
@nitin_agrawal
@nitin_agrawal 6 жыл бұрын
what if we can take diagonal also?code change?
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Yea, the dfs function would be different
@doctor3397
@doctor3397 5 жыл бұрын
Will the algorithm still work if the matrix is like this? 11100 11100 00100 00011 00011 => should return 2 After you count 1 for [0][0] and make the surrounding 1s to 0s. You would have a matrix like this. 00100 00100 00100 00011 00011. You would count again when you come to [0][2]. Your algorithm would return 4.
@dattu06
@dattu06 5 жыл бұрын
After you count 1 for [0][0] and make the surrounding 1s to 0s (recursively). You would have a matrix like this. Once you make the surrounding 1 to 0, you continue to do to its neighbors again 00000 00000 00000 00011 00011. You will count again for [3][3]
@doctor3397
@doctor3397 5 жыл бұрын
@@dattu06 I get it now. Thank you!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
after turning the 1's to 0's, it would return 2. Sorry for the mega late reply lol
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
thanks for explaining it haha
@jorgeacetozi7263
@jorgeacetozi7263 5 жыл бұрын
What's the time complexity for this solution?
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
O(M*N). a year late, but thats what it is haha
@XxBruce5002xX
@XxBruce5002xX 3 жыл бұрын
@@AlgosWithMichael Hey, thanks so much for this video - could you tell me, are M and N the arrays which make up the 2D array? (The array of arrays). And could you tell me the space complexity?
@AbinayaThiyagarajan777
@AbinayaThiyagarajan777 6 жыл бұрын
In this you are changing the current element as well to 0, thus making entire array to zero.Aren't you?
@vasshe
@vasshe 6 жыл бұрын
Yep. That's to avoid infinite recursion. If A is 1 and B (right of A) is 1, when calling the changeLandToWater for B, it will call changeLandTowater(A). If you don't make A as 0, it will again call changeLandToWater(B) and get into infinite recursion.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
a year late... lol but yea, it will turn the array to 0 to avoid infinite loops
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
well explained, thank you
@def__init
@def__init 6 жыл бұрын
Give me more
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
a year late, but i have been posting alot more
@shripalm
@shripalm 6 жыл бұрын
Great explanation
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
thank you
@OFIRROZANES
@OFIRROZANES 5 ай бұрын
what is the time complexity of this solution?
@adeleketinuade3931
@adeleketinuade3931 4 жыл бұрын
Great explanation
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Glad it was helpful!
Technical Interview Question - Max Area of Island [LeetCode]
26:18
AlgosWithMichael
Рет қаралды 9 М.
Amazon Coding Interview Question - Number of Distinct Islands
17:43
AlgosWithMichael
Рет қаралды 26 М.
Noodles Eating Challenge, So Magical! So Much Fun#Funnyfamily #Partygames #Funny
00:33
Молодой боец приземлил легенду!
01:02
МИНУС БАЛЛ
Рет қаралды 1,9 МЛН
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 11 МЛН
They Chose Kindness Over Abuse in Their Team #shorts
00:20
I migliori trucchetti di Fabiosa
Рет қаралды 12 МЛН
Google Coding Interview Question - Number of Closed Islands (LeetCode)
21:03
#Leetcode #200. Number of #Islands || Code + Explanation
14:06
Code with Alisha
Рет қаралды 7 М.
Graph Algorithms for Technical Interviews - Full Course
2:12:19
freeCodeCamp.org
Рет қаралды 1,2 МЛН
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 246 М.
Number of Islands - Leetcode 200 - Graphs (Python)
11:01
Greg Hogg
Рет қаралды 10 М.
DFS vs BFS, When to Use Which?
9:25
AlgoMonster
Рет қаралды 5 М.
NUMBER OF ISLANDS - Leetcode 200 - Python
11:41
NeetCode
Рет қаралды 332 М.
Google Coding Question - Making a Large Island (Hard)
25:11
AlgosWithMichael
Рет қаралды 16 М.
Winning Facebook (Meta) Hacker Cup Qual Round 2022?
53:55
Neal Wu
Рет қаралды 2,6 МЛН
Surrounded Regions | BFS & DFS | LeetCode
19:42
AlgosWithMichael
Рет қаралды 5 М.
Noodles Eating Challenge, So Magical! So Much Fun#Funnyfamily #Partygames #Funny
00:33