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.
@usmanh7704 жыл бұрын
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!
@AlgosWithMichael4 жыл бұрын
Thank you so much, I really appreciate that!
@asgm13822 жыл бұрын
I second this.
@MohammadSalman-zq2rv2 жыл бұрын
By far the best mentor
@koredyte4 жыл бұрын
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!
@AlgosWithMichael4 жыл бұрын
Thank you so much, more videos to come!
@robertsedgewick12663 жыл бұрын
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( )
@AlgosWithMichael3 жыл бұрын
Thank you very much!
@panzabamboo19015 жыл бұрын
Best vid I have seen on this, thanks!
@AlgosWithMichael4 жыл бұрын
thank you
@majetyprashanth15903 жыл бұрын
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 :)
@AlgosWithMichael3 жыл бұрын
Awesome to hear! Thanks for watching
@dimejifaluyi17596 жыл бұрын
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!
@AlgosWithMichael4 жыл бұрын
nice, thanks for watching
@Dragondavid6 жыл бұрын
Man you finally made me to understand recursion!!!!!!!! my hero!!
@AlgosWithMichael4 жыл бұрын
that is exactly what i wanted :)
@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 Жыл бұрын
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-fe2du5 жыл бұрын
Best explanation I have seen. Thanks!
@AlgosWithMichael4 жыл бұрын
Thank you, I appreciate the kind words!
@90krishika5 жыл бұрын
Best explanation on this topic! Loved it (y)
@AlgosWithMichael4 жыл бұрын
glad to hear!
@anpascally5 жыл бұрын
Great video, great explanation. Thank you so much.
@AlgosWithMichael4 жыл бұрын
of course, thank you for watching
@soumilmishracomp4 жыл бұрын
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.
@AlgosWithMichael4 жыл бұрын
Most definitely! Thank you for watching!
@shihaojin53384 жыл бұрын
very clear and helpful. Thanks
@AlgosWithMichael4 жыл бұрын
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!
@tetianao3766 жыл бұрын
such a great explanation!
@AlgosWithMichael4 жыл бұрын
awesome!
@RedionXhepa3 жыл бұрын
Really nice content !
@AlgosWithMichael3 жыл бұрын
Thanks!
@johnkim77835 жыл бұрын
This is a really good explanation! Great job doing in !
@AlgosWithMichael4 жыл бұрын
anytime :)
@snigdhapurohit42166 жыл бұрын
Thank you so much for explaining this problem so well. :)
@AlgosWithMichael4 жыл бұрын
anytime
@DanielVazquez3 жыл бұрын
Good explanation, just a tiny detail: you just missed to change the firsts "ones" occurrences to zeros in the graphic explanation.
@kinjalthehero5 жыл бұрын
Thank you for the clear explanation.
@AlgosWithMichael4 жыл бұрын
No problem!
@pavanomanwar13896 жыл бұрын
Very easy and clear explanation!!
@AlgosWithMichael4 жыл бұрын
awesome to hear
@KazuhiroOinuma5 жыл бұрын
So easy to understand!
@AlgosWithMichael4 жыл бұрын
Glad to hear :)
@dustinv91935 жыл бұрын
Great video. Thanks for your clear explanation.
@AlgosWithMichael4 жыл бұрын
of course!
@ravikumarnarayanan58766 жыл бұрын
Awesome, just made me understand this in one shot.
@AlgosWithMichael4 жыл бұрын
glad to hear
@jeffge80095 жыл бұрын
very clear explanation! thanks
@AlgosWithMichael4 жыл бұрын
awesome, thanks for watching
@ShashwataSaha3 жыл бұрын
For the same algo it's saying maxing recursion depth excided, In pythn
@KnowledgeAmplifier15 жыл бұрын
Very nice explanation ... Clear idea:-)
@AlgosWithMichael4 жыл бұрын
Nice, thanks for watching!
@jingjing68135 жыл бұрын
very clear explanation!!!! Thanks for sharing!
@AlgosWithMichael4 жыл бұрын
no problem
@lylez002 ай бұрын
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.
@mohammadn0man3 жыл бұрын
great explanation
@AlgosWithMichael3 жыл бұрын
Thank you!
@subhadeepbhattacharyya28676 жыл бұрын
I am not sure if your up and down notations are correct.
@anpascally5 жыл бұрын
Yeap. Up should be 'i-1' and Down should be 'i+1'
@AlgosWithMichael4 жыл бұрын
Yea, I messed them up lol
@woodylucas3 жыл бұрын
Every time you ever go over a problem I say to myself "Why did I ever struggle?"
@AlgosWithMichael3 жыл бұрын
haha I'm glad the vids are helping!
@khangt1k255 жыл бұрын
Best video, it's really useful. Thanks!
@AlgosWithMichael4 жыл бұрын
i appreciate that
@SumitKumar-ww7he4 жыл бұрын
Excellent.
@AlgosWithMichael4 жыл бұрын
thank you
@vincenr88226 жыл бұрын
Nice! You should create more videos like this but with other problems. 👍🏻
@AlgosWithMichael4 жыл бұрын
Only 2 years late, but I have been uploading every week now :p
@shameedify3 жыл бұрын
Up and Left recursive calls are not needed the way you are iterating in the caller.
@karthikmoh6 жыл бұрын
Great explanation
@AlgosWithMichael4 жыл бұрын
Thank you!
@ramireddy82236 жыл бұрын
Really great explanation ...
@AlgosWithMichael4 жыл бұрын
i appreciate that
@SameerSrinivas5 жыл бұрын
Great explanation! Could you please tell what is the run time complexity? Both space and time?
@AlgosWithMichael4 жыл бұрын
Both the time and space will be O(M*N). Sorry for the very late reply lol
@MrThepratik5 жыл бұрын
Awsme video Thanks a ton
@AlgosWithMichael4 жыл бұрын
anytime
@qaipak15 жыл бұрын
some home slices be doing this with dem trees dough
@AlgosWithMichael4 жыл бұрын
fuck yea
@udaychatterjee44243 жыл бұрын
Do we need to call the recursive function for up and left? I think calling it for down and right shoulder care of it
@ForCodingInterview3 жыл бұрын
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
@udaychatterjee44243 жыл бұрын
@@ForCodingInterview ahhh. Make sense. Thanks for explaining.
@danielzuzevich41613 жыл бұрын
Thank you.
@AlgosWithMichael3 жыл бұрын
No worries!
@ArghyaBhattacharyaNITA3 жыл бұрын
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..
@DanielVazquez3 жыл бұрын
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.
@DanielVazquez3 жыл бұрын
Is there any advantage of solving this problem using DFS vs BFS?
@MrMayurB5 жыл бұрын
Need help with Most Stones Removed with Same Row or Column - [Leetcode], it is similar problem but could not understand.
@AlgosWithMichael4 жыл бұрын
im going to do a video on that problem
@nagesh0072 ай бұрын
Super
@saulgoodman6710 Жыл бұрын
This solution shouldnt work since you're not considering diagonal cells which are also part of the island right?
@Srikanthking15 жыл бұрын
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
@AlgosWithMichael4 жыл бұрын
ill have to look at this problem, i havent seen it before. thanks for watching!
@marceldegas6 жыл бұрын
When I try this implementation in Ruby, I get "stack too deep" error
@AlgosWithMichael4 жыл бұрын
2 years late lol, but I would say it is because your exit condition is not correct
@india17276 жыл бұрын
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?
@AlgosWithMichael4 жыл бұрын
for this problem, an island does not include diagonals
@milaness27306 жыл бұрын
Very good
@AlgosWithMichael4 жыл бұрын
thank you
@saswatibhattacharjee73875 жыл бұрын
Great explanation. But when I submit this code in Leetcode it throw a error message Java.lang.stackoverflow error. Can you help me?
@rico51465 жыл бұрын
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.
@AlgosWithMichael4 жыл бұрын
a year late lol... but stackoverflow error is usually a problem with your base conditions in your dfs function
@pranupranup82855 жыл бұрын
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??
@AlgosWithMichael4 жыл бұрын
regardless of the size of the array, the number of islands was the same
@MrPrakash2196 жыл бұрын
Good one.
@AlgosWithMichael4 жыл бұрын
thank you
@yafeiwang7305 жыл бұрын
Is this BFS method? Thank you!
@AlgosWithMichael4 жыл бұрын
No, this is the DFS method.
@sifou79745 жыл бұрын
thanks
@AlgosWithMichael4 жыл бұрын
Of course!
@monikajha35006 жыл бұрын
Is column length always grid[0].length ?? I couldnt understand line 7. or should it be grid[i].length? Please help
@niksgupta365 жыл бұрын
grid[0].length is gonna be always same as grid[i].length, so it does not matter. Put anything!
@AlgosWithMichael4 жыл бұрын
since they are the same length of row and column, it wont matter
@AlgosWithMichael4 жыл бұрын
yep, thanks for explaining
@rajathanand935 жыл бұрын
Can you solve K closest points to the Origin? Leetcode - 973
@AlgosWithMichael4 жыл бұрын
Yep, that problem is top on the list.
@nitin_agrawal6 жыл бұрын
what if we can take diagonal also?code change?
@AlgosWithMichael4 жыл бұрын
Yea, the dfs function would be different
@doctor33975 жыл бұрын
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.
@dattu065 жыл бұрын
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]
@doctor33975 жыл бұрын
@@dattu06 I get it now. Thank you!
@AlgosWithMichael4 жыл бұрын
after turning the 1's to 0's, it would return 2. Sorry for the mega late reply lol
@AlgosWithMichael4 жыл бұрын
thanks for explaining it haha
@jorgeacetozi72635 жыл бұрын
What's the time complexity for this solution?
@AlgosWithMichael4 жыл бұрын
O(M*N). a year late, but thats what it is haha
@XxBruce5002xX3 жыл бұрын
@@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?
@AbinayaThiyagarajan7776 жыл бұрын
In this you are changing the current element as well to 0, thus making entire array to zero.Aren't you?
@vasshe6 жыл бұрын
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.
@AlgosWithMichael4 жыл бұрын
a year late... lol but yea, it will turn the array to 0 to avoid infinite loops