Number of islands | Leetcode

Sdílet
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/...

Komentáře • 372

  • @akshatjain6854
    @akshatjain6854 Před 4 lety +248

    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

  • @gauravsrivastavjsr
    @gauravsrivastavjsr Před 4 lety +53

    Your way of solving problems is easy to understand .. thank you for such a nice contents.

  • @HarkiratSaluja
    @HarkiratSaluja Před 4 lety +9

    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.

  • @devesh819
    @devesh819 Před rokem

    Literally u made be this question a cake walk for me . Never studied DFS but your Clear voice filled with knowledge taught me everything

  • @beketmyrzanov1979
    @beketmyrzanov1979 Před 4 lety +7

    You explain way better than some youtubers with thousands subs. Thank you, dude! Keep doing

  • @davidalexander2058
    @davidalexander2058 Před rokem

    Thank you for this crystal clear explanation! I could not follow other explanations found on the internet, but this was very clear.

  • @shritishaw7510
    @shritishaw7510 Před 2 lety +14

    That's an extraordinary explanation mate. You made it look so simple. Thanks a lot.

  • @radhu8
    @radhu8 Před 3 lety +6

    i recently started watching referring your videos. This one is amazing. Such a clear and neat explanation! Thank you for this.

  • @luobohu8128
    @luobohu8128 Před 3 lety +3

    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.

  • @MP-ny3ep
    @MP-ny3ep Před rokem

    Awesome , top notch explanation. Thank you!

  • @KevinN44
    @KevinN44 Před 3 lety +3

    Thank you very much! You have a way of explaining things that makes it very easy to understand 👏🏾👏🏾

  • @codersarena6932
    @codersarena6932 Před 3 lety +2

    good explanation, found a Indian programming tutorial gem :).

  • @varunajmera
    @varunajmera Před 2 lety

    thanks for the amazing explanation. I was unable to find a good video but u explained very well.

  • @AshwaniSharma-of2nq
    @AshwaniSharma-of2nq Před rokem

    Great explanation with simple and efficient solution.

  • @He_ze
    @He_ze Před 7 měsíci

    I like your videos. You explain everything without skipping steps

  • @KarthikaRaghavan
    @KarthikaRaghavan Před 2 lety

    In 2022. This video had a the best explanation for this problem, handsdown! :)

  • @ChandanKumar-em8zg
    @ChandanKumar-em8zg Před 2 lety

    Awesome Explanation. Thanks Sir!

  • @saurabhchauhan232
    @saurabhchauhan232 Před 3 lety

    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

  • @rumaroy219
    @rumaroy219 Před rokem

    You are AMAZING!!!! Best and Simple Explanation!! I am kinda falling short of words! 😀 Thanks Bro.

  • @stupify6751
    @stupify6751 Před 10 měsíci

    dude, you are the best teacher out there!!. Can call you as "Teacher" with out any spelling mistake!!

  • @ritikbhardwaj4061
    @ritikbhardwaj4061 Před 3 lety +4

    Thanks, TechDOSE for this amazing graph series...thanks a lot...

  • @poojakhatri4426
    @poojakhatri4426 Před rokem

    Thank you so much!! I finally understood the problem!

  • @kercat89
    @kercat89 Před 3 lety +3

    Beautifully explained!! Thank you very much for this!

  • @adityathapa
    @adityathapa Před 2 lety

    Thanks man! It was just what I needed.

  • @Jeremy-yb5yo
    @Jeremy-yb5yo Před 3 lety +3

    You always give us the best explanation, Thank you!!!

  • @hemantsood9579
    @hemantsood9579 Před 4 lety +60

    I think the time complexity should be O(n * m) as we are traversing the whole matrix

    • @techdose4u
      @techdose4u  Před 4 lety +13

      Yes.

    • @abhishekjaiswal6492
      @abhishekjaiswal6492 Před 3 lety +1

      @@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

    • @abhishekjaiswal6492
      @abhishekjaiswal6492 Před 3 lety

      please reply

    • @abhishekshankar1136
      @abhishekshankar1136 Před 3 lety +1

      @@abhishekjaiswal6492 each recursive we will do maximum O( 1) work , so I think it's O(nm) , maybe it is > O(nm)

    • @eshaanbhat3732
      @eshaanbhat3732 Před 3 lety +2

      @@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)

  • @yuchenxiao3445
    @yuchenxiao3445 Před 2 lety

    Thank you for your video!

  • @snehasoni1392
    @snehasoni1392 Před 3 měsíci

    it was literally a crystal clear explanation for this question
    thanku TechDose
    u just made my day.....

  • @openworld7585
    @openworld7585 Před 4 lety

    Thank you for very easy way explanations

  • @aniketwattamwar1514
    @aniketwattamwar1514 Před 3 lety +2

    Your content is really good.
    Easy to understand and it helps us to code based on that understanding.
    Please continue making videos.
    Thank you.

  • @decodingParinda
    @decodingParinda Před 4 lety +1

    Couldn't have been more clearer.Thanks for it :)

  • @spetsnaz_2
    @spetsnaz_2 Před 4 lety +3

    There are so many videos on youtube for this problem. Your explanation is the best 🍻

  • @peter-eh2oq
    @peter-eh2oq Před 2 lety

    OMG! Thanks!!!! Please Continue!!!!!!!!!!!!!!

  • @CengizAkarsu
    @CengizAkarsu Před 3 lety +1

    very clear, visual, and direct explanation. Thank you, bro.

  • @sweetsazd11
    @sweetsazd11 Před rokem

    Thank you, this was helpful :)

  • @saurabhsengar9520
    @saurabhsengar9520 Před 4 lety +6

    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.

    • @techdose4u
      @techdose4u  Před 4 lety +2

      Nice..... This technique is something different.

  • @nandanbn7668
    @nandanbn7668 Před 2 lety

    Very well explained thanks man 😍

  • @redleader2211
    @redleader2211 Před rokem +1

    Great solution, and using the graph itself to store the visited state is a big brain move.

  • @KUSUMAKARSHUKLA
    @KUSUMAKARSHUKLA Před rokem

    Thankyou very much. Can't thankyou more!!

  • @SajidAli-ub6th
    @SajidAli-ub6th Před 2 lety +3

    Waiting for disjoint set implementation of the above problem.
    Btw Nice explanation! Thank you!!

  • @shishpalvishnoi7974
    @shishpalvishnoi7974 Před 4 lety +2

    please make video on other graph algorithm.. your way of writing code and explaining is crystal clear.

  • @AnkushKumar-mk8ns
    @AnkushKumar-mk8ns Před 3 lety +2

    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 ).

  • @keshavrastogi5005
    @keshavrastogi5005 Před 3 lety +1

    You are awesome man.... Your solution looks so simple. Thanks for making such videos! :)

  • @harshitsxxd
    @harshitsxxd Před 3 lety +1

    Glad you made this video! great explanation.

  • @AbhayKumarAgrawal
    @AbhayKumarAgrawal Před 4 lety +2

    Thank u so much , all thanks for the way you solve , very easy and understandable, really thanks a lot.

  • @RohitSharma-bu6lc
    @RohitSharma-bu6lc Před 3 lety +1

    Can not thank you enough for the wonderful explanation.

  • @AruneshSrivastava
    @AruneshSrivastava Před 4 lety +2

    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

  • @christophermcmahon9541
    @christophermcmahon9541 Před rokem +1

    I have a benefited a lot from your videos, keep up the good work!

  • @sahildadhich59
    @sahildadhich59 Před měsícem

    very nicely explained

  • @skypravda
    @skypravda Před 3 lety +2

    Thank you, bro, for this clear explanation. 👍

  • @worldwar_two2894
    @worldwar_two2894 Před 9 měsíci

    well done!

  • @Ahmedmeshref12
    @Ahmedmeshref12 Před 4 lety +1

    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?

  • @ManishKumar-ys5oj
    @ManishKumar-ys5oj Před 4 lety +4

    your content helping a lot.....Upload daily more and more problems❤

  • @azeezsayyad7281
    @azeezsayyad7281 Před 4 lety

    great explanation thanks

  • @ManinderSingh-xr6ir
    @ManinderSingh-xr6ir Před rokem

    wonderful explaination

  • @rimzim1032
    @rimzim1032 Před 4 lety +1

    Awesome explanation! Thank you!

  • @mystica7284
    @mystica7284 Před 4 lety +1

    Awesome!!! Subscribed😅. Keep them coming.

  • @575saisri4
    @575saisri4 Před 4 lety +2

    it is a blessing to have found your channel

    • @techdose4u
      @techdose4u  Před 4 lety

      :) when did you find it actually?

    • @575saisri4
      @575saisri4 Před 4 lety

      TECH DOSE recently not sure exactly when 😄

  • @saimeghanabarla9646
    @saimeghanabarla9646 Před 7 měsíci

    Awesome ❤

  • @apcs914
    @apcs914 Před rokem

    good explaination

  • @soneshengg
    @soneshengg Před 3 lety +1

    Thanks!

  • @kunalkakade478
    @kunalkakade478 Před 4 lety +1

    thanks for explaining it in such an easy way !!

    • @techdose4u
      @techdose4u  Před 4 lety

      Welcome :) I like that dp of yours :D

  • @rohitchauhan6170
    @rohitchauhan6170 Před 2 lety +1

    really nice explanation is an easy efficient way thanks a lot

  • @kumarpallav8156
    @kumarpallav8156 Před 3 lety +5

    THE BEST explaination.

  • @malkarajk4093
    @malkarajk4093 Před 4 lety +2

    Sir really super amazing no words to express my happiness keep it up sir

  • @palakgoyal4231
    @palakgoyal4231 Před 2 lety

    sir we are using 2 nested for loops so how can time complexity be N instead of N^2???

  • @sohamdey2177
    @sohamdey2177 Před 3 lety +1

    Simply wow, one of the best solution.

  • @journeytowardslife9830
    @journeytowardslife9830 Před 3 lety +1

    Thank you so much for a detailed explanation 🙂

  • @naveenkumar-ns9sg
    @naveenkumar-ns9sg Před 3 lety +1

    No Words you are Awesome and Genius.. keep doing good problem vedios

    • @techdose4u
      @techdose4u  Před 3 lety

      Not genius...just consistent work :)

  • @shivangishukla2629
    @shivangishukla2629 Před 4 lety +4

    You made this problem as easy as addition of two numbers!! Amazing explanation.

  • @kirthe1116
    @kirthe1116 Před rokem

    The best explanation. I have wasted 1 hour just by seeing other videos

  • @monishrainy8568
    @monishrainy8568 Před 11 měsíci

    great explanation

  • @johnwick-gj9gb
    @johnwick-gj9gb Před 4 lety +1

    great way of doing things ..awesome man

  • @krishnashejul6418
    @krishnashejul6418 Před 3 lety

    Which tool you are using for editing

  • @neelsoni13062
    @neelsoni13062 Před rokem

    Thank You Sir.

  • @shikharjaiswal6877
    @shikharjaiswal6877 Před 3 lety +1

    Superb man! Thanks, for your help.

  • @rajivroy1175
    @rajivroy1175 Před 3 lety

    excellent explanation

  • @yashu6404
    @yashu6404 Před rokem

    thank you so much sir

  • @kunalsoni7681
    @kunalsoni7681 Před 4 lety +1

    Thanking you sir 😊☺️.. to make this problem easy to understand 😊

  • @yynnooot
    @yynnooot Před 2 lety

    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.

  • @kailashjangir1530
    @kailashjangir1530 Před 3 lety

    in gfg it's showing not working for multiple test cases, why?

  • @harshpatel1385
    @harshpatel1385 Před 4 lety +1

    your explanation technique is awesome.

  • @robertyoungie
    @robertyoungie Před 2 lety +1

    Great little tutorial, simplified the problem for me

  • @kannupriyasingh
    @kannupriyasingh Před 3 lety +2

    Best explanation
    Thanku so much :)

  • @Siddharth-yi7pg
    @Siddharth-yi7pg Před 3 lety +1

    Amazing level of explanation and speaking

  • @ubaidmanzoorwani6254
    @ubaidmanzoorwani6254 Před 3 lety

    what is the time complexity for BFS??

  • @codeblooded6760
    @codeblooded6760 Před 2 lety

    To reach all the nodes of the land, why did you use dfs instead of bfs in the helper function?

  • @blueSurfer
    @blueSurfer Před 3 lety +1

    Thanku for making such awesome videos!!

  • @neeraj33negi
    @neeraj33negi Před 4 lety

    nice explanation bro

  • @Highlights_Point
    @Highlights_Point Před rokem +1

    Thnaks sir today i take dose of this question😂😅😅😅

  • @TadesseDev
    @TadesseDev Před rokem

    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.

  • @shubhamgiri2840
    @shubhamgiri2840 Před 4 lety

    Its solving 46/47 test cases. is there any better approach ?

  • @sandeepnallala48
    @sandeepnallala48 Před 3 lety +2

    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 :)

  • @santerek28
    @santerek28 Před 4 lety +1

    Very Well expained sir ! thank you !

  • @bhaskarmahajan98
    @bhaskarmahajan98 Před 4 lety +2

    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

    • @techdose4u
      @techdose4u  Před 4 lety +1

      I told this at the end of the video....that you can solve it with DSU. Please try to watch complete video 😅

    • @bhaskarmahajan98
      @bhaskarmahajan98 Před 4 lety

      @@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.

  • @avadhootjoshi1805
    @avadhootjoshi1805 Před 3 lety +1

    Very calm explanation

  • @atultiwari1547
    @atultiwari1547 Před 4 lety

    good explanation

  • @prabaljain5543
    @prabaljain5543 Před 4 lety +1

    what a great explanation :)

  • @abhaytiwari6411
    @abhaytiwari6411 Před 4 lety +2

    keep it up ..
    welldone

  • @manuramvelappan5935
    @manuramvelappan5935 Před 4 lety +1

    Excellent.. You explained it well...