ACCOUNTS MERGE | LEETCODE # 721 | PYTHON SOLUTION

Sdílet
Vložit
  • čas přidán 13. 03. 2022
  • In this video we are solving a popular Google and Facebook interview question: Accounts Merge (Leetcode 721).
    This is one of my favorite problems because it's a fun graph problem that isn't immediately obviously a graph problem and requires you to modify the given input and transform it into a graph before you can actually solve the problem.
    Stay tuned to find out how we're going to solve this question and make sure to subscribe to the channel!
  • Věda a technologie

Komentáře • 51

  • @Amy-nq3xw
    @Amy-nq3xw Před rokem +4

    Your channel is such a hidden gem. I really like how you don't just solve all the mainstream LC questions but also the ones that are more advanced but useful. Thank you!!

  • @aglovecraft
    @aglovecraft Před 2 lety +11

    This is a great explanation. I watched like 4 other videos of people explaining this problem and you're the only one who took the time to visually represent the problem. That step is so helpful in remembering the logic for these problems. Keep up the great work!

    • @crackfaang
      @crackfaang  Před 2 lety

      Thanks for the kind words! Based on my channel analytics a lot of people just watch the code portions but I’m glad people are getting value from the drawing parts too. Make sure to subscribe so you don’t miss future videos!

  • @rsKayiira
    @rsKayiira Před rokem

    Was stuck on this problem but now I can do it. Thanks a lot. Glad I found the channel.

  • @th2315
    @th2315 Před rokem

    I really like the way you do a very detailed complexity analysis in the end, I learned quite a lot from that, and I don't see other users doing that much. Please keep doing so! Appreciated!

  • @annlaosun6260
    @annlaosun6260 Před 21 dnem

    thank you. after watching so many dfs videos from you. I used a dfs function to code this question and I was able to code it. !

  • @electric336
    @electric336 Před rokem

    Very good explanation. Probably the best I've seen regarding this problem.

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

    Amazing Explanation! Thank you so much!

  • @hulyahare7718
    @hulyahare7718 Před 2 lety +2

    I've watch a lot of videos about this question but I couldn't understand any of them. But your vido is just GREAT, I get it finally! THANK YOU SO MUCHH for showing that it's actually very easy!

  • @fallencheeto4762
    @fallencheeto4762 Před 4 měsíci

    Explanation is amazing as always, just subbed!

  • @jimmyahmed5424
    @jimmyahmed5424 Před 2 lety

    Thanks for explaining!

  • @ebenezeracquah478
    @ebenezeracquah478 Před rokem

    This is great man!. Thank you so much.

  • @user-di2rw4dm5s
    @user-di2rw4dm5s Před 6 měsíci

    i like your sarcastic tone in the video! Keep it up!

  • @nehanarwal2944
    @nehanarwal2944 Před rokem

    Great explanation! Can you also add solution to Calender I problem on leetcode. Thanks!

  • @portiseremacunix
    @portiseremacunix Před rokem

    Thanks! Hope to see comments of the code if possible...somewhere in a github?

  • @user-yt9uj5oi9d
    @user-yt9uj5oi9d Před 10 měsíci

    Nice tutorial! I have a disagreement with the complexity calculation though. We have N accounts -> then N scattered graphs -> ForEach graph, we do traversal(K) + sorting(KlogK) = KlogK, finally O(NK logK). Ignore the nested-loop format, we still traverse every single node in one graph; after this traversal, we do one sorting. We should not have those many multiplications. Please correct me if I'm wrong.

  • @3rd_iimpact
    @3rd_iimpact Před 6 měsíci

    Thanks for this. Will the interviewer be ok with this solution and not union-find?

  • @leetcoderafeeq2641
    @leetcoderafeeq2641 Před rokem

    Thanks!

  • @preethisowjanya5203
    @preethisowjanya5203 Před 17 dny

    thank you!

  • @akhilchandra5935
    @akhilchandra5935 Před 2 lety +2

    lol i love the intro about karma!!

    • @crackfaang
      @crackfaang  Před 2 lety +2

      Hehe got to get you guys to subscribe somehow!

  • @syafzal273
    @syafzal273 Před 4 měsíci

    Love your explanation! I feel like the DFS would've been more intuitive recursively instead of iteratively. (This is your code, with just using recursive DFS)
    class Solution:
    def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
    graph = collections.defaultdict(set)
    # map of email -> name
    email_to_name = {}
    # build a graph
    for account in accounts:
    name = account[0]
    for email in account[1:]:
    # just connect to the first email
    # instead of connecting each email to
    # every other one
    graph[email].add(account[1])
    graph[account[1]].add(email)
    email_to_name[email] = name
    res = []
    visited = set()
    cur = []
    # return all emails connected to the email
    def dfs(email):
    if email in visited:
    return
    visited.add(email)
    cur.append(email)
    for nxt in graph[email]:
    dfs(nxt)
    for email in graph:
    if email not in visited:
    dfs(email)
    res.append([email_to_name[email]] + sorted(cur))
    cur = []
    return res

  • @benjaminnguyen592
    @benjaminnguyen592 Před rokem

    Great explanation! Keep it up man

    • @crackfaang
      @crackfaang  Před rokem

      Thank you! Will do! Make sure to keep watching 😃

  • @user-yk2zu5px3u
    @user-yk2zu5px3u Před rokem +1

    if possible, can you show the recursive way to implement DFS for this question?

    • @hudsonyuen
      @hudsonyuen Před rokem

      This worked for me - not the most space efficient tho
      class Solution(object):
      def accountsMerge(self, accounts):
      """
      :type accounts: List[List[str]]
      :rtype: List[List[str]]
      """
      graph = {}
      emailNameMap = {}
      for account in accounts:
      name = account[0]
      for email in account[1:]:
      if email not in graph:
      graph[email] = set()
      # connect every email to the first one for efficiency
      # we know there will be >= 1 email, so index 1 in bounds
      graph[email].add(account[1])
      graph[account[1]].add(email)
      emailNameMap[email] = name
      visited = set()
      def dfs(email, connected):
      for email in graph[email]:
      if email in visited:
      continue
      visited.add(email)
      connected.append(email)
      dfs(email, connected)
      return connected
      res = []
      for email in graph:
      # return array of all emails connected to that email, including itself
      connected = dfs(email, [])
      # if no connected emails that are unvisited, it's already been added
      if len(connected) == 0:
      continue
      connected.sort()
      # append name to front, then append to res array
      res.append([emailNameMap[email]] + connected)
      return res

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

    Great video. One thing I would nitpick is that you are visiting a node, not an edge.

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

      Yea I tend to mix the terminology. You get the idea though

  • @AmgaaKhosbayar
    @AmgaaKhosbayar Před 7 dny

    For the good karma!

  • @Ninjiazhao2390
    @Ninjiazhao2390 Před 2 lety

    really nice

    • @crackfaang
      @crackfaang  Před 2 lety

      Thanks for the kind words. Make sure to subscribe so you don’t miss future videos

  • @deezeey5811
    @deezeey5811 Před rokem +1

    Thanks for the solution! Btw why is this DFS not BFS?

    • @crackfaang
      @crackfaang  Před rokem +1

      Doesn’t matter. You have to fully explore all paths. Both will work here though I guess when you want to fully explore a path you typically use DFS in theory but it doesn’t matter. Just personal preference here

    • @greta3440
      @greta3440 Před rokem

      I think what he uses is BFS instead of DFS. That's my thought. Correct me if I'm wrong.

    • @greta3440
      @greta3440 Před rokem

      Oh, it's a dfs

  • @sonalverma5981
    @sonalverma5981 Před rokem

    Can it be done using union find algo?

  • @Lily-xp2vb
    @Lily-xp2vb Před 10 měsíci

    i subscribed long time ago, but I need more good karma, please send me good energy!

  • @vigneshsampath3256
    @vigneshsampath3256 Před 2 lety +2

    I subscribed. Hope to get the good Karma.

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

      Thank you! Glad people are hearing the message. If only more people did the same haha I would have a lot more subscribers! 😀

  • @fadsa342
    @fadsa342 Před 11 měsíci +1

    I'm still a little confused about how he created the graph and why you only have to connect each email to the first one in the list instead of connecting each email in the list to all the others

    • @crackfaang
      @crackfaang  Před 11 měsíci +1

      It's simpler to do it this way. You will still be able to traverse the entire graph by only populating that first email. If you were to populate every single possible connection bidirectionally it would drastically increase the processing time and the storage space. There's no reason to do so when only populating the first will allow you to traverse the graph with the same result.
      For this one I'd suggest drawing out an example on a piece of paper and going through the algorithm line by line to understand. That's what helped me when I first came across this problem

  • @markthompson2813
    @markthompson2813 Před rokem

    For the time complexity, wouldn't the sorting time for K items be K Log K? That would make the ultimate Time complexity O( N * K * N * K * Log K) -> O(N^2 K^2 Log K) ?

  • @nguyentuan6108
    @nguyentuan6108 Před rokem

    subscribed!

  • @nehanarwal2944
    @nehanarwal2944 Před rokem

    One question - Why would sorting be Nlog K and not k log k

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

    Thanks for the clear explanation @crackfaang! Could you please confirm the time complexity?

  • @lalalagogo8508
    @lalalagogo8508 Před 3 měsíci

    lol good karma