Number of islands | Leetcode
Vložit
- čas přidán 16. 04. 2020
- This video lecture explains a very important interview programming question which is to find number of islands on a matrix or grid. This is same as finding the number of clusters on a graph or matrix or grid. This is a typical DFS or BFS problem under graph algorithms. This can also be solved using Disjoint Set. I have explained the DFS approach for this problem using examples and code explanation. As usual, CODE LINK is given below. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
CODE LINK: gist.github.com/SuryaPratapK/...
I don't know how to thank you . We are paying money in lacs to college still they don't teach anything. But you are giving us these amazing explanation for free. Thank you so much brother. Keep making videos
Thanks bro :)
well said man.
@@johnwick-gj9gb yup he deserves it
just say "T H A N K Y O U ! ! !" XD
in reality colleges have become randi khhana..u go and pick a girl
Your way of solving problems is easy to understand .. thank you for such a nice contents.
Welcome :)
I went over leetcode explanation, did not get it. Then i went over few of you tube videos where they just solved the question giving minimal explanation. Then i bumped on this. Thank you so much brother. I was trying to understand DFS practically and this pretty much sums it out for a beginner like me. Thanks a bunch again.
Welcome bro :)
Literally u made be this question a cake walk for me . Never studied DFS but your Clear voice filled with knowledge taught me everything
You explain way better than some youtubers with thousands subs. Thank you, dude! Keep doing
Thanks :)
Thank you for this crystal clear explanation! I could not follow other explanations found on the internet, but this was very clear.
That's an extraordinary explanation mate. You made it look so simple. Thanks a lot.
Welcome 😄
i recently started watching referring your videos. This one is amazing. Such a clear and neat explanation! Thank you for this.
Welcome 😀
thank you so much! I love your teaching style. Although many CZcamsrs did algorithm videos, some of them started coding at first without exquisite explanations, so I can't follow them. Thank you again bro.
Welcome :)
Awesome , top notch explanation. Thank you!
Thank you very much! You have a way of explaining things that makes it very easy to understand 👏🏾👏🏾
:)
good explanation, found a Indian programming tutorial gem :).
thanks for the amazing explanation. I was unable to find a good video but u explained very well.
Great explanation with simple and efficient solution.
I like your videos. You explain everything without skipping steps
In 2022. This video had a the best explanation for this problem, handsdown! :)
Awesome Explanation. Thanks Sir!
Thank you, I am so afraid to start any problem that I even couldn't sum two diagonal problem which is easiest. Having around 7 yr of experience as web developer but they way you explain and the code you show made me little confident that logic is most important in this problems
You are AMAZING!!!! Best and Simple Explanation!! I am kinda falling short of words! 😀 Thanks Bro.
dude, you are the best teacher out there!!. Can call you as "Teacher" with out any spelling mistake!!
Thanks, TechDOSE for this amazing graph series...thanks a lot...
Welcome
Thank you so much!! I finally understood the problem!
Beautifully explained!! Thank you very much for this!
Welcome :)
Thanks man! It was just what I needed.
You always give us the best explanation, Thank you!!!
Welcome :)
I think the time complexity should be O(n * m) as we are traversing the whole matrix
Yes.
@@techdose4u but you told in the video the time complexity as O(n). Since we are using dfs for each element, so the time complexity must be greater than O(nxm).What's your opinion? Please clarify it if i am wrong
please reply
@@abhishekjaiswal6492 each recursive we will do maximum O( 1) work , so I think it's O(nm) , maybe it is > O(nm)
@@abhishekjaiswal6492 asume if it's O(m*n) than N = m*n then complexity is O(N) which is again linear time so instead of writing O(m*n) we can directly say it's of O(n)
Thank you for your video!
it was literally a crystal clear explanation for this question
thanku TechDose
u just made my day.....
Thank you for very easy way explanations
Your content is really good.
Easy to understand and it helps us to code based on that understanding.
Please continue making videos.
Thank you.
Welcome
Couldn't have been more clearer.Thanks for it :)
Welcome :)
There are so many videos on youtube for this problem. Your explanation is the best 🍻
Thanks bro :)
OMG! Thanks!!!! Please Continue!!!!!!!!!!!!!!
very clear, visual, and direct explanation. Thank you, bro.
Welcome :)
Thank you, this was helpful :)
awesome bro...
Another method I thought was to first find the indices of all the 1's and store it in list l.
Then traverse through the list in dfs and keep on removing the elements from the list l if found ...
But 😅one of the test case went out of time...46/47.
Nice..... This technique is something different.
Very well explained thanks man 😍
Great solution, and using the graph itself to store the visited state is a big brain move.
👍🏽
Thankyou very much. Can't thankyou more!!
Waiting for disjoint set implementation of the above problem.
Btw Nice explanation! Thank you!!
Sure 👍🏼 Thanks
please make video on other graph algorithm.. your way of writing code and explaining is crystal clear.
Thanks...will do it soon
Sir , If grid contains all 1's then , complexity wiil be O(n* m) + O (complexity for the recursiver calls done in 1st iteration)... ? Will it be still O(n * m ).
You are awesome man.... Your solution looks so simple. Thanks for making such videos! :)
Welcome :)
Glad you made this video! great explanation.
Thanks
Thank u so much , all thanks for the way you solve , very easy and understandable, really thanks a lot.
Welcome :)
Can not thank you enough for the wonderful explanation.
Welcome :)
This is good explanation, suppose if going diagonally is also allowed . then will this work if we just add these 4 more directions to the existing 4 directions
mark_current_island(matrix,x-1,y-1,r,c); // LEFT UP DIAGONAL
mark_current_island(matrix,x-1,y+1,r,c); //RIGHT
UP DIAGONAL
mark_current_island(matrix,x+1,y-1,r,c); //LEFT DOWN DIAGONAL
mark_current_island(matrix,x+1,y+1,r,c); //RIGHT
DOWN DIAGONAL
I have a benefited a lot from your videos, keep up the good work!
Nice :)
very nicely explained
Thank you, bro, for this clear explanation. 👍
Welcome :)
well done!
When I tried the same alg but changing the if statement in the main function to if (grid[I][j] ==0): continue,
else: dfs () and no_of_islands += 1
The output was not right.
Also for the recursive call, when I changed the last condition of the if statement to (matrix[x][y] == 0), I got a max depth error.
I have no idea why am I getting that behavior knowing that when I leave the two conditions as in the video, it works fine. Does anyone know why?
can you post your code here?
your content helping a lot.....Upload daily more and more problems❤
Thanks :)
great explanation thanks
wonderful explaination
Awesome explanation! Thank you!
Welcome :)
Awesome!!! Subscribed😅. Keep them coming.
Thanks :)
it is a blessing to have found your channel
:) when did you find it actually?
TECH DOSE recently not sure exactly when 😄
Awesome ❤
good explaination
Thanks!
thanks for explaining it in such an easy way !!
Welcome :) I like that dp of yours :D
really nice explanation is an easy efficient way thanks a lot
Welcome :)
THE BEST explaination.
Thanks :)
Sir really super amazing no words to express my happiness keep it up sir
Thanks :)
sir we are using 2 nested for loops so how can time complexity be N instead of N^2???
Simply wow, one of the best solution.
Thanks :)
Thank you so much for a detailed explanation 🙂
Welcome :)
No Words you are Awesome and Genius.. keep doing good problem vedios
Not genius...just consistent work :)
You made this problem as easy as addition of two numbers!! Amazing explanation.
Thanks :)
The best explanation. I have wasted 1 hour just by seeing other videos
great explanation
great way of doing things ..awesome man
Thanks :)
Which tool you are using for editing
Thank You Sir.
Superb man! Thanks, for your help.
Welcome :)
excellent explanation
thank you so much sir
Thanking you sir 😊☺️.. to make this problem easy to understand 😊
Welcome :)
Thank you for your video. I am learning about traversals now, but I don't understand how this is a DPS solution. Don't DPS solutions use a Stack? I'm also curious why you chose DFS over BFS.
in gfg it's showing not working for multiple test cases, why?
your explanation technique is awesome.
Thanks :)
Great little tutorial, simplified the problem for me
Thanks 😄
Best explanation
Thanku so much :)
Welcome :)
Amazing level of explanation and speaking
Thanks
what is the time complexity for BFS??
To reach all the nodes of the land, why did you use dfs instead of bfs in the helper function?
Thanku for making such awesome videos!!
Welcome
nice explanation bro
Thnaks sir today i take dose of this question😂😅😅😅
Great lesson brother, I have an issue to discuss though. Is this better to use DF over BF, I use BF and my solution raises time limitation issues.
Its solving 46/47 test cases. is there any better approach ?
a simple thanks is not enough sir .for such amazing content. LEARNING MANY NEW CONCEPTS from your videos sir.. keep giving DOSE of TECH to us sir : ) ... really love your videos sir : )
if any one watching your video for first time, defnetly they wil subscribe sir :)
Very Well expained sir ! thank you !
Welcome
Just wanted to know if we can do this problem with union find( disjoint sets) concept ?
I tried to do that but can't come up with any good implementation
I told this at the end of the video....that you can solve it with DSU. Please try to watch complete video 😅
@@techdose4u oh sorry! Actually I knew this solution thats why I skipped rest of the video( when you said that it can be done using DFS/BFS)
But as I was not able to come up with DSU solution, I wanted to know its solution and complexity
And will it give better complexity than BFS and DFS
If you can tell please reply.
Very calm explanation
Thanks
good explanation
what a great explanation :)
Thanks :)
keep it up ..
welldone
Thanks :)
Excellent.. You explained it well...
Thanks :)