Number of Islands - Breadth First Search (LeetCode)

Sdílet
Vložit
  • čas přidán 28. 07. 2024
  • 📚 Graph Problem Tips Video: • Coding Interview Prep ...
    🎧 Join the community Discord: / discord
    💰 Support me on Patreon: / michaelmuinos
    🔗Follow me on LinkedIn: / michael-muinos
    📂Follow me on Github: github.com/MichaelMuinos
    Check out my interview prep platform for learning the patterns!
    📢 Interview Prep Platform: algoswithmichael.com
    Number of islands is one of the most popular questions on Leetcode and in coding interviews in general. It is a graph problem that has been known to be asked at many top companies such as Amazon, Google, Facebook, Microsoft, Apple, Bloomberg, Adobe, Uber, and many more.
    Number of islands is a graph based question where we must count the islands in a 2D matrix containing only 0's and 1's where 1's are land and 0's are water. An island is any 1's connected horizontally or vertically together, but not diagonally. To solve this problem, you can use a BFS, DFS, or union find algorithm, but for this tutorial I go over the breadth-first search approach. For a BFS, we must initialize a queue to add the position we are currently looking at to continue adding any neighbors in the up, down, left, and right directions. Anytime we encounter a 1 (land), then we add 1 to our count for an island and change it to water so that we don't double count the same island.
    The time complexity of our solution is going to be O(N*M) where N is the number of rows and M is the number of columns in our matrix. The space complexity will be O(min(N*M)) where N is our row length and M is our column length.
  • Věda a technologie

Komentáře • 59

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

    You have a really nice crystal clear way of explaining things, please keep making videos!

  • @mangoman2023
    @mangoman2023 Před 2 lety

    One of the best explanations. Clear and concise. Thank you

  • @FitnessChaos
    @FitnessChaos Před 3 lety

    Dude nice, simple and clean. Watched other tutorials but couldn't understand them.

  • @andrejoljaca3577
    @andrejoljaca3577 Před 3 lety

    Big upgrade from your previous explanations a couple months back with the whiteboard. Definitely one of the best explainers on youtube with these new videos! Keep up the great work!

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

    Thank you for always discussing time and space complexities. Keep it up!

  • @AmitKumar-xr6cy
    @AmitKumar-xr6cy Před 3 lety +1

    I really appreciate the effort you have put to make the video and also explanation was on point

  • @mcreyfonacier1982
    @mcreyfonacier1982 Před 3 lety

    I love the in depth explanation with illustrations.

  • @norbertaberor2514
    @norbertaberor2514 Před rokem

    This dudes videos are 101% the best on DSA you'll find anywhere 🔥👍🚀

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

    coordinates is such smart idea

    • @AlgosWithMichael
      @AlgosWithMichael  Před 3 lety

      Yea, the first time I learned it, I was like 😱😱😱😱

  • @davidobodo3605
    @davidobodo3605 Před rokem

    Thank you very much, The graphical explanation at the start really made it very clear

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

    Brilliant. Please do video every day. It would be of great help!!

  • @tengma1020
    @tengma1020 Před 3 lety

    Superb Job. learnt quite a bit. thank you.

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

    Nice job @Michael Muinos

  • @jitendrakumar-vv8ho

    i solved it using dfs technique and was quite easy method

  • @harinidhanasekaran8202

    Thank you so much!!

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

    Nice! Vote for 2 videos/week.

  • @asmahamdym
    @asmahamdym Před rokem

    thanks for the video
    what is the point of using a queue when it will always have one value ?

  • @Steve-ji5lr
    @Steve-ji5lr Před 2 měsíci

    I remembered you have one DFS solution before

  • @grb47
    @grb47 Před 14 dny

    in R*(col) + C what if size of C > R then it will give extra values

  • @sumishajmani705
    @sumishajmani705 Před 2 lety

    will the queue always have a length of one?

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

    Very well explained... Thanks for making it easy for us... Why you din't use recursion instead like the one you used in closed island?

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

      I do have another (older) video going over this problem using recursion. I wanted to specifically cover the BFS approach in this video

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

    Whats the point of doing 2d to 1d and 1d to 2d

  • @arunraju9705
    @arunraju9705 Před 3 lety

    Kind bars? Awesome !

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

    Can you make a video on path with maximum probability . By the way your explanation is awesome

  • @sase1017
    @sase1017 Před 3 lety

    the optimum solution, better than other youtubers and their skills is not at your level yet.

    • @AlgosWithMichael
      @AlgosWithMichael  Před 3 lety

      Haha thank you! I don't know though some of those competitive programmers would wipe the floor with me

    • @AdpYTExplorer
      @AdpYTExplorer Před 3 lety

      @@AlgosWithMichael but you taught this problem better than them, take heart in that!

  • @interviewprep6827
    @interviewprep6827 Před 3 lety

    Can you please please please do Number of Islands II?? I can't get to understand solutions explained by anyone else other than u...

  • @yitingg7942
    @yitingg7942 Před 3 lety

    Hi Michael, can you please also do one for 547. Friend Circles since it's very similar to this question but different. However I find it very hard to understand the difference. Thank you so much in advance!

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

      Definitely, I was planning on doing that problem

    • @yitingg7942
      @yitingg7942 Před 3 lety

      @@AlgosWithMichael Thank you so much Michael!!

  • @codeblooded6760
    @codeblooded6760 Před 8 měsíci

    Why did you use bfs and not dfs?

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

    High-caliber content!

  • @tiffaniejohnson352
    @tiffaniejohnson352 Před 3 lety

    why don't we increase islands in the if statement in fillWithWater?

    • @AlgosWithMichael
      @AlgosWithMichael  Před 3 lety

      We are increasing islands, but right before we call the `fillWithWater` function. All attached land to the first encounter of land will get filled if that makes sense

  • @danielmcdonald7272
    @danielmcdonald7272 Před 2 lety

    Where is the link in the description 2d to 1d array conversion?

    • @AlgosWithMichael
      @AlgosWithMichael  Před 2 lety

      It is in the description, but I'll link it here as well :)
      czcams.com/video/tMSdjPKFRD4/video.html

  • @pixaz
    @pixaz Před 3 lety

    I can't find the URL for the 2D -> 1D coordinate :(

    • @xxsanyxx1
      @xxsanyxx1 Před 2 lety

      czcams.com/video/tMSdjPKFRD4/video.html

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

    I thought you and Kevin Naughton are the same person

  • @davidtheprogrammer
    @davidtheprogrammer Před 2 lety

    I think there's too many references to previous content and it makes this pretty hard to follow as a person landing on this video directly.