Technical Interview Question: Number of Islands [LeetCode]

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 พ.ย. 2024

ความคิดเห็น • 142

  • @usmanh770
    @usmanh770 4 ปีที่แล้ว +8

    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!

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

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

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

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

  • @panzabamboo1901
    @panzabamboo1901 5 ปีที่แล้ว +7

    Best vid I have seen on this, thanks!

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

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

  • @johnkim7783
    @johnkim7783 5 ปีที่แล้ว

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

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

  • @90krishika
    @90krishika 5 ปีที่แล้ว +1

    Best explanation on this topic! Loved it (y)

  • @Ryan-fe2du
    @Ryan-fe2du 5 ปีที่แล้ว

    Best explanation I have seen. Thanks!

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

  • @tetianao376
    @tetianao376 6 ปีที่แล้ว +1

    such a great explanation!

  • @woodylucas
    @woodylucas 3 ปีที่แล้ว

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

  • @pavanomanwar1389
    @pavanomanwar1389 6 ปีที่แล้ว

    Very easy and clear explanation!!

  • @RedionXhepa
    @RedionXhepa 3 ปีที่แล้ว

    Really nice content !

  • @DanielVazquez
    @DanielVazquez 3 ปีที่แล้ว

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

  • @snigdhapurohit4216
    @snigdhapurohit4216 6 ปีที่แล้ว +1

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

  • @anpascally
    @anpascally 5 ปีที่แล้ว +1

    Great video, great explanation. Thank you so much.

  • @dustinv9193
    @dustinv9193 5 ปีที่แล้ว

    Great video. Thanks for your clear explanation.

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

  • @lylez00
    @lylez00 13 วันที่ผ่านมา

    I've been interviewing lately, and I'm not encountering any of these "common questions" I'm seeing on TH-cam. 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!

  • @jingjing6813
    @jingjing6813 5 ปีที่แล้ว

    very clear explanation!!!! Thanks for sharing!

  • @ravikumarnarayanan5876
    @ravikumarnarayanan5876 6 ปีที่แล้ว

    Awesome, just made me understand this in one shot.

  • @jeffge8009
    @jeffge8009 5 ปีที่แล้ว

    very clear explanation! thanks

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

  • @kinjalthehero
    @kinjalthehero 5 ปีที่แล้ว

    Thank you for the clear explanation.

  • @lylez00
    @lylez00 หลายเดือนก่อน

    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.

  • @KnowledgeAmplifier1
    @KnowledgeAmplifier1 5 ปีที่แล้ว

    Very nice explanation ... Clear idea:-)

  • @KazuhiroOinuma
    @KazuhiroOinuma 5 ปีที่แล้ว

    So easy to understand!

  • @karthikmoh
    @karthikmoh 6 ปีที่แล้ว +1

    Great explanation

  • @ShashwataSaha
    @ShashwataSaha 3 ปีที่แล้ว

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

  • @mohammadn0man
    @mohammadn0man 3 ปีที่แล้ว

    great explanation

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

  • @khangt1k25
    @khangt1k25 5 ปีที่แล้ว

    Best video, it's really useful. Thanks!

  • @ramireddy8223
    @ramireddy8223 6 ปีที่แล้ว

    Really great explanation ...

  • @shameedify
    @shameedify 3 ปีที่แล้ว

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

  • @SumitKumar-ww7he
    @SumitKumar-ww7he 4 ปีที่แล้ว +1

    Excellent.

  • @DanielVazquez
    @DanielVazquez 3 ปีที่แล้ว

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

  • @MrThepratik
    @MrThepratik 5 ปีที่แล้ว

    Awsme video
    Thanks a ton

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

  • @qaipak1
    @qaipak1 5 ปีที่แล้ว

    some home slices be doing this with dem trees dough

  • @danielzuzevich4161
    @danielzuzevich4161 3 ปีที่แล้ว

    Thank you.

  • @yafeiwang730
    @yafeiwang730 5 ปีที่แล้ว

    Is this BFS method? Thank 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.

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

  • @saulgoodman6710
    @saulgoodman6710 ปีที่แล้ว

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

  • @MrPrakash219
    @MrPrakash219 6 ปีที่แล้ว

    Good one.

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

  • @nagesh007
    @nagesh007 หลายเดือนก่อน

    Super

  • @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 ปีที่แล้ว +1

      im going to do a video on that problem

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

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

  • @nitin_agrawal
    @nitin_agrawal 6 ปีที่แล้ว

    what if we can take diagonal also?code change?

    • @AlgosWithMichael
      @AlgosWithMichael  4 ปีที่แล้ว

      Yea, the dfs function would be different

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

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

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

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

  • @def__init
    @def__init 6 ปีที่แล้ว

    Give me more

    • @AlgosWithMichael
      @AlgosWithMichael  4 ปีที่แล้ว

      a year late, but i have been posting alot more

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

  • @shripalm
    @shripalm 6 ปีที่แล้ว

    Great explanation

  • @OFIRROZANES
    @OFIRROZANES 4 หลายเดือนก่อน

    what is the time complexity of this solution?

  • @adeleketinuade3931
    @adeleketinuade3931 4 ปีที่แล้ว

    Great explanation