Technical Interview Question: Number of Islands [LeetCode]

Поділитися
Вставка
  • Опубліковано 29 вер 2024
  • In this video, I explain the popular coding interview question Number of Islands.
    🎧 Join the community Discord: / discord
    💰 Support me on Patreon: / michaelmuinos
    🔗Follow me on LinkedIn: / michael-muinos
    📂Follow me on Github: github.com/Mic...
    This problem is asked at Google, Amazon, Facebook, and many other companies.
    The idea behind the algorithm is to use a DFS traversal to change all land to water. Each time we encounter land, we will tally up how many islands their are. This problem can be solved using a BFS or Union Find algorithm, but I find the Depth First Search solution the easiest to understand.

КОМЕНТАРІ • 141

  • @koredyte
    @koredyte 4 роки тому +2

    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!

  • @dimejifaluyi1759
    @dimejifaluyi1759 6 років тому +1

    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!

  • @Dragondavid
    @Dragondavid 6 років тому +1

    Man you finally made me to understand recursion!!!!!!!! my hero!!

  • @RedionXhepa
    @RedionXhepa 3 роки тому

    Really nice content !

  • @anpascally
    @anpascally 5 років тому +1

    Great video, great explanation. Thank you so much.

  • @snigdhapurohit4216
    @snigdhapurohit4216 6 років тому +1

    Thank you so much for explaining this problem so well. :)

  • @kinjalthehero
    @kinjalthehero 5 років тому

    Thank you for the clear explanation.

  • @usmanh770
    @usmanh770 4 роки тому +7

    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!

  • @karthikmoh
    @karthikmoh 6 років тому +1

    Great explanation

  • @DanielVazquez
    @DanielVazquez 3 роки тому

    Is there any advantage of solving this problem using DFS vs BFS?

  • @milaness2730
    @milaness2730 6 років тому

    Very good

  • @yafeiwang730
    @yafeiwang730 5 років тому

    Is this BFS method? Thank you!

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

  • @def__init
    @def__init 6 років тому

    Give me more

  • @subhadeepbhattacharyya2867
    @subhadeepbhattacharyya2867 6 років тому +17

    I am not sure if your up and down notations are correct.

    • @anpascally
      @anpascally 5 років тому +2

      Yeap. Up should be 'i-1' and Down should be 'i+1'

    • @AlgosWithMichael
      @AlgosWithMichael  4 роки тому

      Yea, I messed them up lol

  • @shripalm
    @shripalm 5 років тому

    Great explanation

  • @lylez00
    @lylez00 18 днів тому

    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.

  • @ShashwataSaha
    @ShashwataSaha 3 роки тому

    For the same algo it's saying maxing recursion depth excided, In pythn

  • @shameedify
    @shameedify 3 роки тому

    Up and Left recursive calls are not needed the way you are iterating in the caller.

  • @saulgoodman6710
    @saulgoodman6710 Рік тому

    This solution shouldnt work since you're not considering diagonal cells which are also part of the island right?

  • @panzabamboo1901
    @panzabamboo1901 5 років тому +7

    Best vid I have seen on this, thanks!

  • @OFIRROZANES
    @OFIRROZANES 3 місяці тому

    what is the time complexity of this solution?

  • @pogostemoncablin4507
    @pogostemoncablin4507 2 роки тому +1

    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.

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

  • @nagesh007
    @nagesh007 21 день тому

    Super

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

  • @DanielVazquez
    @DanielVazquez 3 роки тому

    Good explanation, just a tiny detail: you just missed to change the firsts "ones" occurrences to zeros in the graphic explanation.

  • @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 роки тому +2

      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.

  • @KnowledgeAmplifier1
    @KnowledgeAmplifier1 5 років тому

    Very nice explanation ... Clear idea:-)

  • @woodylucas
    @woodylucas 3 роки тому

    Every time you ever go over a problem I say to myself "Why did I ever struggle?"

  • @90krishika
    @90krishika 5 років тому +1

    Best explanation on this topic! Loved it (y)

  • @danielzuzevich4161
    @danielzuzevich4161 3 роки тому

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

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

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

  • @MrMayurB
    @MrMayurB 5 років тому

    Need help with Most Stones Removed with Same Row or Column - [Leetcode], it is similar problem but could not understand.

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

  • @mohammadn0man
    @mohammadn0man 3 роки тому

    great explanation

  • @rajathanand93
    @rajathanand93 4 роки тому

    Can you solve K closest points to the Origin? Leetcode - 973

  • @tetianao376
    @tetianao376 5 років тому +1

    such a great explanation!

  • @SumitKumar-ww7he
    @SumitKumar-ww7he 4 роки тому +1

    Excellent.

  • @sifou7974
    @sifou7974 4 роки тому

    thanks

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

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

  • @SameerSrinivas
    @SameerSrinivas 5 років тому

    Great explanation! Could you please tell what is the run time complexity? Both space and time?

    • @AlgosWithMichael
      @AlgosWithMichael  4 роки тому +1

      Both the time and space will be O(M*N). Sorry for the very late reply lol

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

  • @khangt1k25
    @khangt1k25 5 років тому

    Best video, it's really useful. Thanks!

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

  • @dustinv9193
    @dustinv9193 5 років тому

    Great video. Thanks for your clear explanation.

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

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

  • @Ryan-fe2du
    @Ryan-fe2du 4 роки тому

    Best explanation I have seen. Thanks!

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

  • @jingjing6813
    @jingjing6813 5 років тому

    very clear explanation!!!! Thanks for sharing!

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

  • @nitin_agrawal
    @nitin_agrawal 6 років тому

    what if we can take diagonal also?code change?

  • @johnkim7783
    @johnkim7783 5 років тому

    This is a really good explanation! Great job doing in !

  • @jeffge8009
    @jeffge8009 5 років тому

    very clear explanation! thanks

  • @KazuhiroOinuma
    @KazuhiroOinuma 4 роки тому

    So easy to understand!

  • @qaipak1
    @qaipak1 5 років тому

    some home slices be doing this with dem trees dough

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

  • @ravikumarnarayanan5876
    @ravikumarnarayanan5876 5 років тому

    Awesome, just made me understand this in one shot.

  • @adeleketinuade3931
    @adeleketinuade3931 4 роки тому

    Great explanation

  • @pavanomanwar1389
    @pavanomanwar1389 6 років тому

    Very easy and clear explanation!!

  • @MrThepratik
    @MrThepratik 5 років тому

    Awsme video
    Thanks a ton

  • @ramireddy8223
    @ramireddy8223 6 років тому

    Really great explanation ...

  • @MrPrakash219
    @MrPrakash219 6 років тому

    Good one.