Medium Facebook Graph Interview Question - Max Area of Island - Leetcode 695

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ส.ค. 2024
  • FAANG Coding Interviews / Data Structures and Algorithms / Leetcode

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

  • @GregHogg
    @GregHogg  4 หลายเดือนก่อน +24

    Master Data Structures & Algorithms For FREE at AlgoMap.io!

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

      Congratulations for the video! When I get some free time, I'll send my C++ solution here

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

      I want to say tracing coastlines could save time, but I think the conditions in which it would might be too specific to bother

  • @TF2Shows
    @TF2Shows 4 หลายเดือนก่อน +58

    You can optimize it: instead of using "visited" set, you can modify in-place the matrix. Set visited cells to '' (empty string) instead of 1.

    • @GregHogg
      @GregHogg  4 หลายเดือนก่อน +17

      You could absolutely do that. Although it doesn't save too much time because the hashset lookup is also O(1). Yours doesn't use hashing though which would indeed be a bit faster

    • @lOmaine777
      @lOmaine777 4 หลายเดือนก่อน +6

      Didn't some recruiter comment over here that in place modification of input is a red flag?

    • @davidespinosa1910
      @davidespinosa1910 4 หลายเดือนก่อน +8

      @@lOmaine777 Good memory ! That was in Greg's solution to Leetcode 200 (th-cam.com/video/9dOFm8am4Qs/w-d-xo.html). Yes, an interviewer said that -- recruiters don't usually code. 🙂 In production code, input modification is a no-no. In programming contests, it's totally fine. In interviews, it's best to ask the interviewer. 👍

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

      @@lOmaine777data should be immutable but say if the data is mutable then inplace operations are much better

    • @ProfRoxas
      @ProfRoxas 4 หลายเดือนก่อน +1

      you can also use an [n,m] boolean array which also O(1) time for lookup and while it might take more space when there's fewer island, you do save up on hashing when you lookup and insert in the set.

  • @chixenlegjo
    @chixenlegjo 4 หลายเดือนก่อน +9

    I think I have an O(mn) time and space solution that doesn’t require DFS or a hashset. It involves storing the size of each island in a list (beginning with just [0]), and as you march through the list (left-to-right and up-to-down), you check the value of the cell, the of the above cell, and the value of the cell to the left. If the current cell is 0, nothing happens and it stays 0. If both above and left of the current cell is 0, set the cell to the length of the sizes list, and then append a 1 to it. If exactly one of the cells is a 0, add 1 to the element in the list with the index of the nonzero value, then set the cell’s value to the same as the nonzero cell. If both are nonzero, add the list’s element indexed above to the value indexed to the left plus one, then set the value indexed above to 0. Set the current cell’s value to that of what’s to the left. By the end this process, you get a list of the sizes of the islands, some of which are 0 because they are duplicates. The maximum size of this list is mn/2 if the grid has a checkerboard pattern. I’ll write some pseudocode in a reply.

    • @chixenlegjo
      @chixenlegjo 4 หลายเดือนก่อน +2

      grid[m][n] = …
      sizes = [0]
      ans = -1
      for 0

    • @davidespinosa1910
      @davidespinosa1910 4 หลายเดือนก่อน +2

      @@chixenlegjo Unless the problem asks for constant space, most programmers value simplicity over space usage. BTW, you might like Antti Laaksonen's book, Guide to Competitive Programming -- it's quite good. Also, did you run your code ? Leetcode has an easy-to-use interface, and it's fun.

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

      Interesting solution! Does it work though when the islands are non-convex in the 'up/left' direction? E.g. an 'n' or an 'F' shaped island. I'm not totally convinced that the index stored in grid[r][c] will be updated correctly in all cases, or if sometimes the program can hit an outdated one.
      I could easily believe it works, though, it's just hard to prove it to myself!

  • @unstopper3258
    @unstopper3258 4 หลายเดือนก่อน +1

    Is it possible to do it by using Union-find for less space complexity? (not sure about comparing time complexity)

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

      Yeah won't change the time complexity... Doesn't union find use space for the ranks and parents though?

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

    Hello, at my university to work with graphs and their algorithms we use the networkX Python library. Do you recommend it to work with these data structures? Which one do you recommend for this type of problem to solve? Greetings and thanks for the video.

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

      Are you sure that's not for networking? These are just normal graphs from a data structures point of view

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

      networkX is for graph problems

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

    If there are 2 with the largest size how does this return both of them?

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

      The question wants the size, not the list of coordinates that make up the island. So it would be the same number either way. Great question

  • @Wojtek_Sech
    @Wojtek_Sech 3 หลายเดือนก่อน +2

    me: return 5;

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

      That ain't gonna work homie (I know you know that haha)

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

    what is the name of your drawing app?

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

      Miro

  • @user-dv2et9ij3z
    @user-dv2et9ij3z 3 หลายเดือนก่อน

    Wave Li algotithm

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

    But what does DSF mean?

    • @frederikja2210
      @frederikja2210 3 หลายเดือนก่อน +3

      Its suppposed to be DFS (depth first search)

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

      @@frederikja2210 Thanks bro

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

    Floodfill?

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

      Sorta... But moreso in general just a search!

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

    Reminds me of kmaps

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

      What's a kmap?

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

      ​@@GregHogg not much, what's k with you?

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

      @@GregHoggsomething used to minimize Boolean Equations for designing digital circuits

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

    Is this code logic also used by Minesweeper ? 😅

    • @GregHogg
      @GregHogg  4 หลายเดือนก่อน +2

      Sorta, yeah! When you press a key in minesweeper, it would do a dfs or bfs to expose all it can in that connected component of where you clicked :)

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

    What application you used to run python

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

      Oh. Here this is the leetcode environment

  • @insert0name0here91
    @insert0name0here91 4 หลายเดือนก่อน +2

    could you make it so you let us try to solve the question on our own before you give the answer

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

      A lot of these problems are on LeetCode, I google them and try to solve them myself, if it doesn’t work I watch these!

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

      As soon as you see code on the screen, hit the J key. That's the keyboard shortcut for "back up 10 seconds". Or look away from your phone ! 🙂

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

      @@davidespinosa1910 it’s too late at that point sometomes

  • @user-ff5xx3lf7s
    @user-ff5xx3lf7s 3 หลายเดือนก่อน

    n^4😂

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

      Hmm?

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

    Mate I’m sick of seeing your videos. These sorts of videos only appear after Meta, Google, etc do massive layoffs.

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

      Sorry to bother you with them lol

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

    yeah unsubbed, as your answers are only mostly correct.

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

      Is there a problem with Greg's code ? It looks fine to me.

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

      Yeah, this one is correct though...

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

      Yeah there's nothing wrong with this code.