Accounts Merge - Leetcode 721 - Python

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

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

  • @guilhermezardo7671
    @guilhermezardo7671 ปีที่แล้ว +92

    Definitely not a medium problem. Should be a hard problem in my opinion...

    • @christendombaffler
      @christendombaffler ปีที่แล้ว +6

      It's a common Big N interview question, so it's a demoted Hard. Great if you're interested in those companies, awful otherwise, should probably be the final problem in the Union-Find section of the neetcode advanced structures course either way.

  • @DavidDLee
    @DavidDLee ปีที่แล้ว +7

    Interesting solution to use the account index as the nodes being joined in the graph.
    I too used a Union-Find solution but used the email strings as the nodes instead. Using the index, it is pretty trivial to find the name. In my solution, I needed a map from parent -> name.
    Part of the problem is that Union-Find provides as output a non-useful child -> parent tree, which you need to invert into parent -> children.
    The last loop can be done using list comprehension in one line.

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

    10:52 I don't understand how union find operations take only constant time? Worst case would be log n isn't it?

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

      With path compression in the find() operation, it takes O(1) time is what he meant to say I guess, though I am not too sure.

    • @hb-fm1oe
      @hb-fm1oe 7 หลายเดือนก่อน

      its O(1) given the combined optimizations of path compression in find() + the maintaining of a somewhat balanced tree he's doing using the multiple 'if' statements in union()

  • @technophile_
    @technophile_ ปีที่แล้ว +5

    I'm just happy that I'm able to at least understand the terminologies used in this video 😅

  • @huzayfasabri9691
    @huzayfasabri9691 ปีที่แล้ว +9

    Wait what was the time and space complexity?

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

    Thanks for great work!

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

    The neatness of ur code makes u neetcode😊

  • @atreides4911
    @atreides4911 ปีที่แล้ว +27

    What a coincidence, I was working on this one. Are you doing grind75 problems now??

  • @anishkarthik4309
    @anishkarthik4309 ปีที่แล้ว +6

    please make solutions for leetcode weekly contests here onwards if possible please, your explanations are very easy and understandable. No one on TH-cam explains better than you.
    So I can learn to solve weekly problems

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

    Solved this problem like 3 days ago. It was a pain in the butt to get my head around using dfs with two different references.

  • @devenderbhatt421
    @devenderbhatt421 ปีที่แล้ว +4

    It would be better if u link ur union find explanation video in description itself very hectic to find that in ur old library😢

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

      Did you find it? Im looking for it also

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

    If you're looking for today's daily LC (Design Add and Search Words Data Structure) 👉th-cam.com/video/BTf05gs_8iU/w-d-xo.html

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

    Love to you from India sir ❤️

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

    Bro make videos on leetcode contests too

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

    Cann't we just use DFS instead of using disjoint sets. ? Whats the efficiency in using disjoint sets, anyways we have to traverse all connected components.

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

    @NeetCode what is the time&space complexity ?

    • @kolhesatish
      @kolhesatish 3 วันที่ผ่านมา

      N is length of account and m is big biggest size of emails in single list
      O(NMlogNM)

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

    This is definitely a hard problem.

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

    Thank you

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

    This is definitely a hard problem

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

    Love your vids bro. Is the time complexity O(NKlogK) where N accounts, K max-emails in an account, as for the DFS solution?

  • @sheikhmkrifat7749
    @sheikhmkrifat7749 6 วันที่ผ่านมา +1

    oh god why am i so dumb?

  • @SHAZIALI-gw8bh
    @SHAZIALI-gw8bh 8 หลายเดือนก่อน

    This one should be hard 🤯

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

    This one of tough af

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

    kind request to solve weekly contest

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

    please make solutions for leetcode weekly contests

  • @Daniel-do2mh
    @Daniel-do2mh ปีที่แล้ว

    Hi man! Move your content and thank you very much for helping us! Would love if u could also make playlists here of "Easy" , "Medium" and "Hard" related to how the leetcode problems solved in the videos are.

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

    bad variable naming

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

    first

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

    mEdiUm...

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

    The path compression of find() in your code is not actually compressing path, here is the one that actually compress:
    def find(self, node):
    # Path Compression
    parentIdx = self.parents[node]
    while parentIdx != self.parents[parentIdx]:
    parentIdx = self.parents[parentIdx]
    self.parents[node] = parentIdx
    return parentIdx

    • @chrisdafa
      @chrisdafa ปีที่แล้ว +4

      it is compressing path. It's partial path compression. It basically updates every 2nd element to points to its grandparent. So thereby halving the path to the root parent essentially. It's kinda good because it achieves compression but not as aggressively as full path compression, thus the number of write operations is not as many when calling find(), therefore in some cases it can be more efficient when u call find