ACCOUNTS MERGE | LEETCODE # 721 | PYTHON SOLUTION

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ม.ค. 2025

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

  • @Amy-nq3xw
    @Amy-nq3xw 2 ปีที่แล้ว +7

    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 2 ปีที่แล้ว +13

    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  2 ปีที่แล้ว

      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!

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

    0:22 bro casually fear mongered me into subscribing, it was effective!

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

    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!

  • @hulyahare7718
    @hulyahare7718 2 ปีที่แล้ว +3

    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!

    • @crackfaang
      @crackfaang  2 ปีที่แล้ว +1

      Glad you found it useful!

  • @谢雨潇
    @谢雨潇 ปีที่แล้ว +4

    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.

  • @rsKayiira
    @rsKayiira 2 ปีที่แล้ว

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

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

    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 2 ปีที่แล้ว

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

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

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

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

    Love this explanation !!!!

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

    Explanation is amazing as always, just subbed!

  • @rachnaramkumar
    @rachnaramkumar 2 ปีที่แล้ว +1

    Amazing Explanation! Thank you so much!

  • @3rd_iimpact
    @3rd_iimpact ปีที่แล้ว +1

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

  • @TooManyPBJs
    @TooManyPBJs 9 หลายเดือนก่อน +1

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

    • @crackfaang
      @crackfaang  9 หลายเดือนก่อน +2

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

  • @ebenezeracquah478
    @ebenezeracquah478 2 ปีที่แล้ว

    This is great man!. Thank you so much.

  • @akhilchandra5935
    @akhilchandra5935 2 ปีที่แล้ว +3

    lol i love the intro about karma!!

    • @crackfaang
      @crackfaang  2 ปีที่แล้ว +2

      Hehe got to get you guys to subscribe somehow!

  • @portiseremacunix
    @portiseremacunix 2 ปีที่แล้ว

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

  • @무야호-w8g
    @무야호-w8g 2 ปีที่แล้ว +1

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

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

      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

  • @markthompson2813
    @markthompson2813 2 ปีที่แล้ว +1

    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) ?

  • @jimmyahmed5424
    @jimmyahmed5424 2 ปีที่แล้ว

    Thanks for explaining!

  • @deezeey5811
    @deezeey5811 2 ปีที่แล้ว +1

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

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

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

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

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

      Oh, it's a dfs

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

      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

  • @benjaminnguyen592
    @benjaminnguyen592 2 ปีที่แล้ว

    Great explanation! Keep it up man

    • @crackfaang
      @crackfaang  2 ปีที่แล้ว

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

  • @nehanarwal2944
    @nehanarwal2944 2 ปีที่แล้ว

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

  • @sonalverma5981
    @sonalverma5981 2 ปีที่แล้ว

    Can it be done using union find algo?

  • @nehanarwal2944
    @nehanarwal2944 2 ปีที่แล้ว

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

  • @vigneshsampath3256
    @vigneshsampath3256 2 ปีที่แล้ว +2

    I subscribed. Hope to get the good Karma.

    • @crackfaang
      @crackfaang  2 ปีที่แล้ว +2

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

  • @Lily-xp2vb
    @Lily-xp2vb ปีที่แล้ว

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

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

    thank you!

  • @leetcoderafeeq2641
    @leetcoderafeeq2641 2 ปีที่แล้ว

    Thanks!

  • @nguyentuan6108
    @nguyentuan6108 2 ปีที่แล้ว

    subscribed!

    • @crackfaang
      @crackfaang  2 ปีที่แล้ว

      Welcome to the channel

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

    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

  • @NinjiaJoeLife
    @NinjiaJoeLife 2 ปีที่แล้ว

    really nice

    • @crackfaang
      @crackfaang  2 ปีที่แล้ว

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

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

    For the good karma!

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

    Need the good karma

  • @SeanKearney-g7d
    @SeanKearney-g7d 3 หลายเดือนก่อน

    cool

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

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

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

    lol good karma