Komentáře •

  • @AlgoJS
    @AlgoJS Před 3 měsíci +8

    Streamline your learning today - algojs.dev 🚀

  • @nitrogenez
    @nitrogenez Před měsícem +231

    who tf uses a string literal for arrays of 1s and 0s 😫

    • @redestroyer7994
      @redestroyer7994 Před měsícem +39

      JavaScript developers, I guess.

    • @SVGc1993
      @SVGc1993 Před měsícem +3

      Yeah right? Why not [true, false, true, true, false] 😂

    • @redestroyer7994
      @redestroyer7994 Před 27 dny +1

      @@SVGc1993 `0`s and `1`s would work too, if you map 'em afterwards. Saves typing.

    • @SVGc1993
      @SVGc1993 Před 26 dny +1

      @@redestroyer7994 bruh I was kidding xD Obviously 1 and 0 as integers is the way to go, but still, true and false are a better approach than "1" and "0" xD

    • @serb1146
      @serb1146 Před 24 dny

      JavaScripters...

  • @petarpercic6457
    @petarpercic6457 Před 28 dny +26

    The question is "Find the number of connected components in a graph?", google for graph theory behind it.

  • @Snurklll
    @Snurklll Před měsícem +6

    WHY LIGHTMODE MY EYEEESS

  • @JordanMetroidManiac
    @JordanMetroidManiac Před měsícem +4

    It wouldn’t be as efficient as a series of DFS searches, but I think you can also solve this problem as a cellular automaton. Make it so that outside 1’s are set to zero until only one 1 remains for each island (then count number of ones when CA stops changing state). Some stopping condition would have to be checked to make sure that an island doesn’t disappear completely, but the idea’s correct and should work just as effectively, though not as efficiently.

  • @bradenm3
    @bradenm3 Před 5 dny +1

    It would be very helpful for you to define the question

  • @tanvirahmed7993
    @tanvirahmed7993 Před 2 měsíci +145

    you should explain how you came up with the solution first. Any fool can explain an already written code

    • @AlgoJS
      @AlgoJS Před 2 měsíci +31

      That’s why I’ve attached the long form to the video for a more in-depth breakdown. Thanks for watching

    • @djbootymeatlive1484
      @djbootymeatlive1484 Před 2 měsíci

      Yea unfortunately this upload isnt about learning or solving anything, it’s about getting you to click a link.

    • @onlyforyou9999
      @onlyforyou9999 Před měsícem +9

      Bro don't insult like this anyone please 🥺

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

      @@AlgoJS Attached the long form solution where?
      I can’t see a video description for shorts.
      Is there a 2nd video?
      Very interesting problem

    • @AlgoJS
      @AlgoJS Před měsícem +4

      @@robertweekes5783 Above the title of this short there is "Number of Island - Leetcode 200 - JavaScript". If you click that it'll direct you to the long form 👍

  • @Opno
    @Opno Před 6 dny

    Love your stuff man, I get excited at every new upload no matter the wait or the topic. I just love engaging with your thought process and writing. Although the travel videos are my personal favorite

  • @thekwoka4707
    @thekwoka4707 Před 12 dny +1

    I'd kind of prefer doing a flood fill that when it hits land it circles it then continues flood filling. Should be less overall work....depending on I guess the average size of islands.

  • @colinbrash
    @colinbrash Před 3 dny

    My favorite interview question: given a bunch of strings tell me all the galaxies!

  • @myne00
    @myne00 Před 3 dny

    Minesweeper comes to mind

  • @thewelder3538
    @thewelder3538 Před 28 dny +5

    Recursive is such a terrible way to solve this problem, it's just the easiest. Yeah, don't do it like that.

    • @GrouchierThanThou
      @GrouchierThanThou Před 12 dny +1

      Yeah, this example is a steaming pile of shit. You can easily solve this by looping through the grid and just checking the cell before and above the current one in each iteration.

  • @gerhitchman
    @gerhitchman Před měsícem +2

    just use convolution, easy, solved

  • @smgjreinieren
    @smgjreinieren Před 12 dny +1

    Those function names…

  • @SteelyGlow
    @SteelyGlow Před 6 dny

    1. Calculate the size of a row.
    2. Calculate the row number.
    3. Count every '1' in the matrix (in one pass), simultaneously checking the values of nearby elements if needed.
    4. ???
    5. PROFIT

  • @666nevermore
    @666nevermore Před měsícem +11

    Using matrices is pretty slow, if you use dictionaries which as key has Vecoyr2Int you could talk about “offsets” concepts. Add a direction utility variable of the type u want that holds Vector2Int for offset ( (1,0) (-1,0) (0,1) (0,-1) ). At that point you don’t need any nested loop, you just check for neighbors existence within your utility of Vector2Int (assuming in your value you have some class of type Land or something, otherwise if you don’t need you can just use an HashSet). This way the code is much faster, also more elegant and small.

    • @dominobuilder100
      @dominobuilder100 Před měsícem +5

      No no no. Indexing an array (and hence ”matrix” which is a nested array) is constant time and ridiculously fast. Introducing a hashmap here is just going to slow the code down a lot.

    • @thelaagernation9532
      @thelaagernation9532 Před 28 dny

      ​@@dominobuilder100it ends up depending on how big the matrix is. What determines the time at the end of the day is how long the loops are accessing dead space (not islands). Once the matrices get big enough, it becomes an issue, especially if the islands are sparse

    • @dominobuilder100
      @dominobuilder100 Před 27 dny

      @@thelaagernation9532 what are you talking about? You can solve the problem in O(N) with super fast constant time factor because accessing the tile type is just a matter of an array lookup. Lookups in arrays are ridiculously fast no matter how big they are. This is basic

    • @thelaagernation9532
      @thelaagernation9532 Před 27 dny

      @@dominobuilder100 ... it's a multidimensional array...

    • @thelaagernation9532
      @thelaagernation9532 Před 27 dny

      @@dominobuilder100 it is also "basic" knowledge that using a matrix graph representation is not necessarily better than an adjacency list. It depends on the problem and in this case an adjacency list will probably work faster. Array lookups are fast, but your overall run time, believe it or not, still depends on size

  • @sgetti4dinner
    @sgetti4dinner Před 26 dny

    Such a nice short wpwp.

  • @kavinyudhitia
    @kavinyudhitia Před měsícem +8

    It reminds me, long time ago a friend of mine called this as a "flood fill" problem. Because it works by "flooding" the "island". Not sure if that is a right terminology 😂😂😂😂

  • @kunodragon4355
    @kunodragon4355 Před měsícem +3

    Why do you need to return anything from `dfs()`? Seems to me the outermost call, from `numIslands()`, will always return `true`. The recursive `dfs()` calls may return false but you don't even check those. Seems you are using it to clear out the rest of the island, instead of actually validating anything. Did I miss something? I like your approach btw

    • @justinnguyen523
      @justinnguyen523 Před 12 dny

      Yeah there’s essentially no point in having it return any value which leaves the inner if statement useless since you can just increment the numOfIslands and do DFS to clear out the island

  • @bhBlacky82
    @bhBlacky82 Před 6 dny

    use new standards, no longer counts in for loops.. just for (var index in array) {}

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

    This was my Amazon interview question

  • @sebastiancypert2877
    @sebastiancypert2877 Před měsícem +3

    Couldn't you convolve it with a kernel that has a -4 in the center and 1s in all 4 directions and then check for -4s in the answer? Basically just do a laplacian convolution.

    • @Turalcar
      @Turalcar Před 24 dny +1

      Nice. That's exactly what I just came up with.

    • @Turalcar
      @Turalcar Před 24 dny

      That exact thing doesn't sound right though: you find only 1-tile islands that way

    • @Turalcar
      @Turalcar Před 24 dny +1

      It looks like an island with a lake inside would be a counterexample.

  • @RickGladwin
    @RickGladwin Před 4 dny

    Mutating the data from the original grid is a big no for me.

  • @gamerk316
    @gamerk316 Před měsícem +19

    God, I hate these. All Algorithm questions get you are math experts and language experts, not people who know how to architect a system.

    • @82TheKnocKY
      @82TheKnocKY Před 28 dny +3

      This is none of these examples. It doesn't require a math skill nor language specific expertise, just understanding of basic algorithms that faang developer should have

    • @Turalcar
      @Turalcar Před 24 dny

      System design interviews are hard so before having one it is reasonable to weed out people who don't know basic stuff.

    • @austinyun
      @austinyun Před 13 dny

      Interview problem: I'm trying to cache or store things on finite sized shards. How do I minimize the number of times individual shards need to get information from outside their local scope?
      I would bet my left nut that some variant of this problem comes up in system architecture all the time.

  • @error53ish
    @error53ish Před 21 dnem

    This particular solution does assume that you're allowed to change the data in the matrix though... So be aware of that.

  • @BigPapaMitchell
    @BigPapaMitchell Před měsícem +12

    this question and all questions like this are stupid and terrible interview questions. Being able to solve code challenges is a practiced skill thats irrelevant to 99.9% of developers on the planet. Design, debugging and maintainability are much more important.

    • @andrii6292
      @andrii6292 Před měsícem +2

      plus here

    • @cholobok
      @cholobok Před 24 dny +2

      I wouldn’t say 99.99%. More like 80% maybe. It also improves reasoning skills, at least it did for me.

    • @ENCRYPTaBIT
      @ENCRYPTaBIT Před 18 dny

      I've been a developer for 8 years at 2 enterprise companies. And I would have agreed with you for 7.9 of those years. (Started in front end web development, last place I was a full stack software engineer). I've recently bit the bullet and started actually looking at shit like this differently. The questions are being presented like this so that we don't have to do the abstraction ourselves, but there are PLENTY of real world scenarios for a lot of DSA stuff. The problem is, it's presented as theory first, and never tied to practicality (or usually way way way too late if at all). Example, going through my algorithm course for my 5th attempt at Learning this stuff I realized I need to Constantly ask myself how can this be applied to what I've come across either from a ticket or in our codebase. If I can't think of anything I'll literally Google "real world use case for X" and add a link and a screenshot to my notes. I didn't know that RingBuffers/ArrayQueues can be used for buffering video data, and the fact I heard Primeagen say he used it at netflix makes me that much more excited about knowing that.
      The problem is that leetcode and these types of questions try to take the magic programmers are ACTUALLY responsible for which is taking a problem and finding a way to programmatically solve it, and this usually only happens after the first step in figuring out how to represent the data you have to work with as a starting point. Of course, there's no practicality without meaning and who gives a shit about whether or not I can invert a binary tree.... we'll the answer is those who are smart enough to see the opportunities to apply what they've learned

    • @matheusadornidardenne8684
      @matheusadornidardenne8684 Před 18 dny

      Exactly. I'd even say it is more likely to bias the process towards favoring students with no experience instead of senior developers.

    • @cholobok
      @cholobok Před 18 dny

      @@matheusadornidardenne8684 Isn’t that a good thing though?

  • @sgetti4dinner
    @sgetti4dinner Před 26 dny +2

    Just a thought, could we use a hash set to keep track of our visited “1” positions. Then as we loop through the grid and find a 1 we check the position to the left and the position above our location against the visitedOnes hash set. If either position is found in the visitedOnes hash set then we know that our current location is connected to an island we have already counted, so add the current location to the visitedOnes hash set and just continue traversing. If the left and top positions are not in the vistedOnes hash set then we have run into a new island, so increment totalCount by 1 and add the current location to the visitedOnes hash set.
    I think this would work because of the way we traverse over the grid

    • @Turalcar
      @Turalcar Před 24 dny

      hash set is slower than just checking a value in a grid

    • @sgetti4dinner
      @sgetti4dinner Před 24 dny

      @@Turalcar It wouldn’t work anyway, but I think it’s quicker in the worst case. Worst case for dfs is O(2mn) I think. The worst case is it’s one big island. You would stop at [0][0] then you would recursively search the entire grid because everything is a 1 technically. Then when you return to the original nested look outside of dfs you still have to traverse the entire grid of water.
      If the harshest did work (which my solution above does not work) it would technically run on O(mn) time. My particular solution doesn’t work, however a hash set would speed up things in the worst case if someone went overkill to make it work. But, again making it work is not very practical.

    • @sgetti4dinner
      @sgetti4dinner Před 24 dny

      This doesn’t work btw. I left a comment on why the other day but it never showed.

    • @Turalcar
      @Turalcar Před 24 dny +1

      @@sgetti4dinner They are the same O(mn) (constants are meaningless there). but 2 lookups are still less than one hash calculation.

    • @sgetti4dinner
      @sgetti4dinner Před 24 dny

      @@Turalcar yeah constants are irrelevant in big O. This originally came from me just trying to apply a diff approach to the problem.

  • @user-oc8tc2wx5t
    @user-oc8tc2wx5t Před 13 dny

    guys what coding app ? or program is used here

  • @maxisource
    @maxisource Před 13 dny

    I deadass thought Billie joe Armstrong was about to start teaching me c

  • @Ford-ju9yr
    @Ford-ju9yr Před 3 měsíci +8

    Nice!
    And what if you cant modify map? Is there any better way do it than doing full copy of the map and then carry on modifying your own copy of it?

    • @nigh_anxiety
      @nigh_anxiety Před 3 měsíci +7

      Haven't tested this to verify, but it might be faster to make a hashmap of already visited positions.

    • @Ford-ju9yr
      @Ford-ju9yr Před 3 měsíci +1

      @nigh_anxiety time complexity is the same for hashmap, but memory wise it could be more effective if there are a lot of 0 on the map

    • @bloom945
      @bloom945 Před 3 měsíci +3

      You would need to somehow store if each tile has been visited or not, so you would need O(m*n) space. You could do it with a 'visited' HashSet.

    • @mathtsai899
      @mathtsai899 Před 2 měsíci

      Add visit tags to all point

    • @enzoraynal1216
      @enzoraynal1216 Před 2 měsíci +1

      Visit grid left to right top to bottom. If land doesnt have land left or top then its a new island

  • @ciscou
    @ciscou Před 9 dny

    I wasn't expecting any kind of hate in the comments of this video TBH 😕

    • @L3monsta
      @L3monsta Před 4 dny

      Since the beginning of coding everyone has been a critic

  • @karituurihalme1007
    @karituurihalme1007 Před 15 dny +2

    Based on that island definition the example has zero islands. Island NEED to be surrounded by water by that defination. There is only a continent there.

  • @ooroshaza
    @ooroshaza Před 2 měsíci +7

    Isn't union-find is used for this kind of problems?

    • @Splish_Splash
      @Splish_Splash Před 2 měsíci +1

      yep, i solved it using DSU

    • @AnasImloul
      @AnasImloul Před 2 měsíci +1

      yup, however the DFS solution is more optimal since there is no underlying logic (union-find logic) other than traversing

  • @badhombre4942
    @badhombre4942 Před 19 dny +1

    Not only is this incorrect, the code is inefficient.

    • @ciscou
      @ciscou Před 9 dny

      Care to explain how this is incorrect?

  • @Exodus_Dev
    @Exodus_Dev Před 8 dny

    the white background...

  • @matheusadornidardenne8684

    I am convinced that the people who come up with these coding challenges for interviews haven't actually worked as developers. Especially if you're not allowed to research online.
    I'd take as a better proof of seniority if the developer researched the problem for a couple of hours, read papers and posts about different solutions, pitched to me what he concluded is the best one with the pros and cons, and then implemented it with best practices.

    • @Xynic48
      @Xynic48 Před 13 dny

      It's not really solely about if you are able to solve it. Often times, they'd want to hear your thought process on how you problem solve to find your way to a solution. Speaking out your thoughts significantly improves your chances of getting hired.

    • @matheusadornidardenne8684
      @matheusadornidardenne8684 Před 13 dny

      @Xynic48
      Hearing our thought process without "let me read the docs" or "I bet there is a better solution" is barely representative of how we work day to day.

    • @Xynic48
      @Xynic48 Před 13 dny

      @@matheusadornidardenne8684 That's true if you are applying as a developer for a random company, but often times, these random companies care more about your stack and experience rather than if you can solve coding problems. But if you are applying for a big tech company like google or facebook, you will be making the documentation not the other way around. They might assign you more programming-related work instead of just developing, hence they need to know if you can solve problems related to data structures and algorithms.

  • @333CJ2daK
    @333CJ2daK Před 3 měsíci +1

    ok

  • @centripetal6157
    @centripetal6157 Před 16 dny

    Other than a puzzling problem to solve... When will any of this be used in actual real life solution to a business problem?

    • @Haibrayn42
      @Haibrayn42 Před 14 dny

      Minesweeper game maybe

    • @centripetal6157
      @centripetal6157 Před 14 dny

      @@Haibrayn42 So its not practical at all... Got it.

    • @austinyun
      @austinyun Před 13 dny +1

      Brother really. I'm not even a software engineer. I'm an electrician. But I took a few courses in college. Every island is a set of vertices in a graph connected with the rest in the island and not connected to any other vertices. If that doesn't immediately tell you that this could come up in something like network traffic routing you need to find a new career.
      Edit: thought for another minute. Imagine the vectors and edges are mutual links in websites or retweets or something. Honestly. This should immediately strike you as the kind of problem an engineer should know how to solve, but more importantly, be able to see how real life problems are represented by basic graphs.

    • @centripetal6157
      @centripetal6157 Před 13 dny

      @@austinyun nice shaming tactics. Real nice. However your explanation of network traffic makes very little sense since a graph structure would be horribly unbalanced with clusters of islands - thus making search and traversal extremely inefficient, nor does it make sense in a website or frontend sense.
      If your talking about a graph structure such as a tree, it also makes no sense for scalability. From an engineer point of view its impractical to do this.
      Also as stated in the problem these are arrays of values being grouped, not vectors (which are dynamic). Not sure what engineering purpose your trying to solve.
      Leetcode questions don't make you an engineer. These are more like puzzle questions

    • @austinyun
      @austinyun Před 13 dny

      @@centripetal6157 if you can't even find real world examples of finding strongly connected components and connected graphs with Google I just feel sorry for you

  • @erikvdpluijm
    @erikvdpluijm Před 17 dny

    seems inefficient. what if you start in the middle, and spread outward, give each land square a number, make it the lowest of any connected land pieces to the current square. do that recursively. needs 1 pass i think.

  • @aiden_wang2022
    @aiden_wang2022 Před 2 měsíci

  • @sentinelaenow4576
    @sentinelaenow4576 Před 27 dny +1

    Any interview with these kind of questions are completely stupid, because they measure absolutely nothing relevant, specialy when this is trivial for most LLMs today.

  • @StuartLoria
    @StuartLoria Před 2 měsíci +1

    Horrible variable names though

  • @Nightlurk
    @Nightlurk Před 4 dny

    This is way to complicated and non-performant. There's a much simpler non-recursive and more performant way of doing this using an array and an array index, with a single linear iteration over all cells top-left to bottom-right, applying a simple "look left" and "look up" rule.

  • @mathtsai899
    @mathtsai899 Před 2 měsíci +4

    Honestly, this problem is super easy.

    • @AlgoJS
      @AlgoJS Před 2 měsíci +3

      Confidence level 100, I like it

    • @simonwillover4175
      @simonwillover4175 Před 2 měsíci

      Not if you are trying to do it with multiple threads.

    • @impro9118
      @impro9118 Před 2 měsíci

      @@simonwillover4175 Nothing more difficult as creating a thread for every row in the grid

    • @mlgswagman6002
      @mlgswagman6002 Před 2 měsíci

      This makes it more interesting, what are you thinking for a solution?
      I'm thinking that to add parallelism, you split the entire grid into subgrids that each thread will work on, and basically have each thread do the standard solution on the subgrid.
      However, a problem arises when islands span across subgrids. So, Each subgrid will be responsible for making sure islands aren't double counted by the subgrids directly below and to the right of it. When a thread visits a cell, it will mark it with a unique tag indicating what island it is apart of. So when a different thread reaches a boundary of its subgrid, and recognizes that one of its islands extends to the subgrid below / to the right of it, it can use the unique tag info to make the appropriate amount of decrements to the global amount of islands count.
      You need a little extra logic to handle when a thread reaches a subgrid boundary, but the thread responsible for the adjacent subgrid hasnt tagged the relevant cells yet, and of course the nitty gritty of using atomics.

  • @nytr
    @nytr Před 21 dnem

    Idk if its a joke but your solution really does not make sense to me

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

    You're not checking the diagonals. Also, there's no need for recursion at all. It's a simple linear problem. Study John Conway's "Life" algorithm.

    • @mg7753
      @mg7753 Před 29 dny +1

      The problem statement only assumes vertical or horizontal connections. Can you elaborate on your linear solution with the Life algorithm? I'm curious.

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

    This was literally my Algorithm Analysis Homework last week lol. Out of body experience seeing this short

  • @nilfux
    @nilfux Před 5 dny

    impractical nonsense.

  • @4teapo
    @4teapo Před 2 měsíci

    This is way too easy

  • @1231213sdasd
    @1231213sdasd Před 2 měsíci +1

    So if someone wants to be a half decent software developer he has to have a 150iq? Great, life is soooo fair…

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

      No, you use problems you have learned as tools for similar problems. Imagine you were learning this and didn't understand;. Then you look up each line of code what it does and how it helpes you with your solution. You dont have to be a genius to understand things that you take time to study! Once you understand more concepts, you can identify the patterns in your new problem that may occurred in past problems you had to solve. A good example is learning how binary searches work. Once yo understand recursion and when its useful (divide part of divide and conquer) and understand how using a another function that uses pointers to compare values as a current index(conquer method) you can use this logic and patterns forever! You dont need to be a genius to implement it, you already studies the concept of binary search. You can think of this problem as similar. There is a pattern here you have yet to be exposed to. Of course you won't recognize what to do. And neither do i for that matter! But you can see that this problem builds on other concepts apparent in binary search algorithms and perhaps others. All it means is that you need to expose yourself to easier concepts at first and then keep building on those concepts. Good luck man! And remember, even experienced developers look up solutions or methods to solve problems,. You are, as a developer, expected to teach yourself things to solve new problems. Important to remember ! People in the comments who say its "easy" are jut people who have been exposed to this beefore and understand at a fundamental level what tools need to be used. It isn't "easy" because they are geniuses, it is easy because they recognize patterns they have used in previous problems and know how to implement them with this problem!

    • @Broly91571
      @Broly91571 Před měsícem +3

      Life isn't fair. The faster you learn that, instead of whining making the best use of your abilities the faster you can succeed.

    • @JordanMetroidManiac
      @JordanMetroidManiac Před měsícem +3

      That’s not true, obviously. A good software dev would almost certainly have more than 100 IQ but 150 would make them desire to do more than software development.

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

      Nope... you just need to be excellent at finding the information you need, just be an expert on how to use google/chatgpt and you'll be top tier dev in no time