LeetCode 721. Accounts Merge [Coding Interview Solution Explained]

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

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

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

    I was doing well on the Grind75 list until this problem just crushed my confidence. I don't think I've ever used union find and wouldn't have recognized it, and the graph approach I was attempting had me going in circles. Thanks for the excellent explanation!

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

      Yes union find is not super common but it does come up every now and then :) I’m glad the video helped!

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

    This video really helped me with this Leetcode problem.
    I probably didn't code exactly the way you did, but I have a solution beating 61.97% of submissions and better than 93.09% of solutions in memory usage.
    I think using lambda helped a lot for removing duplicates in the accounts that did not need to be merged.
    Thanks a ton!

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

      That’s awesome:) glad it helped!

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

    The explanation I was looking for!!!! ❤️❤️

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

    Thanks for the solution. One point is that if you use ordered map for email2AccountIdx, you wouldn't need to sort the vector in the end.

    • @ShiranAfergan
      @ShiranAfergan  3 ปีที่แล้ว

      You’re right :) You could do that. Then the first loop will take longer because insert to map is log(n) vs insert to hash table which is average constant. But you’re avoiding the sort in the end. Complexity will be the same but if there are many duplicate emails, it will probably be better to use hash and sort at the end. Because you’re sorting less elements than what you insert to the table. What do you think?

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

    The way you write code is amazing, please upload a video on what to keep in mind before writing a code and how to write an efficient code like you

    • @ShiranAfergan
      @ShiranAfergan  3 ปีที่แล้ว

      This type of video is definitely on my list :) thanks 😊

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

    Can you please create video on union-find data structure?

  • @SumitKumar-fn3gj
    @SumitKumar-fn3gj 3 ปีที่แล้ว +2

    Thank you very much for this video. You teach very well. I have a hard time solving this but your way of teaching makes me understand very well and I solve it my own without seeing code of yours. Again thank you very much.

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

      That’s awesome! I’m glad it helped! :)

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

    I was struggling a lot with this problem. Thanks for the explanation.

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

      You’re welcome. I’m glad it helped:)

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

    Good explanation. It'll be more helpful if you could add comments to your code. Thanks for the great effort though.

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

    Nice explanation Shiran, now i am feeling more confident in DSU, thanks!

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

      That’s awesome 👏🏼 👏🏼 glad it helped :)

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

    Very well explained ! You earned a subscriber. Cheers!

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

    Great explanation. Thanks

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

    Thanks

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

    Can you share your screen writing setup ? Looks great

    • @ShiranAfergan
      @ShiranAfergan  3 ปีที่แล้ว

      Thanks :) It’s an iPad Pro and the app is GoodNotes

  • @ilovemikey-777
    @ilovemikey-777 2 ปีที่แล้ว

    Valuable video ! Thank you very much !

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

    "After a million compilation errors" - Yeah totally relatable. Damn!!😂😂

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

    Great video. Can you also make a video on how you learn all the algorithms or prepare interview questions in general?

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

      Thanks Jiadong! That’s a good idea, I’ll try to think on this type of video

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

      @@ShiranAfergan I am searching for the same in your play list too. I like the Data Structure. Would be also helpful if you do one Data Structure with simple examples programmatically. I know too much, but would help even beginners.

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

    Amazing explanation, thanks!

    • @ShiranAfergan
      @ShiranAfergan  3 ปีที่แล้ว

      Thanks :) I’m glad it helped

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

    Very well explained. Thanks a lot.

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

    Great. Could you please make some more videos on problems related to union find . Any problem on leetcode that would be good to grasp the concept better.

    • @ShiranAfergan
      @ShiranAfergan  3 ปีที่แล้ว

      I have another union-find problem on my channel. check out “most stones removed with same row or column”
      th-cam.com/video/beOCN7G4h-M/w-d-xo.html

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

    Hello Shiran Nice video. It's still such a contrived solution for this problem, it feels like without having seen the problem before you would naturally go for some kind of DFS approach though.

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

      I see what you mean, and you could do dfs here. The complexity will be similar so it’s still a good solution but in practice it will be slower because you would have to construct the graph

    • @jlecampana
      @jlecampana 3 ปีที่แล้ว

      @@ShiranAfergan Thank you for your reply Shiran! I have noticed that Union-Find it's a such a prevalent topic for interviews (or at least in coding practice platforms like LeetCode it is), but do you know if Union-Find is common enough so that a Question that REQUIRES it pops up in a FAANG interview? I'm not sure if I should invest my time in learning it fully is worth it? or I could do well with Graph traversals alone. What do you think? Also, I'd be glad to be able to connect with you on Linkedin, hope it's possible. Take care and thank you for these awesome videos, keep it up!

    • @jlecampana
      @jlecampana 3 ปีที่แล้ว

      @@ShiranAfergan You're really great at explaining tough Interview Questions, Would you mind doing LeetCode 809 - Expressive Words next? That one is trickier than it looks IMHO and Google asks it during interviews. Have a nice week!

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

      I don’t think union-find should be your top priority. Graphs are more important. But I would at least learn the basics of it - which operations it can do, what’s the time complexity and basic implementation.
      Sure, we can connect on LinkedIn, send me an invite.
      Thanks for the suggestion (LeetCode 809) I will look into this :)

    • @jlecampana
      @jlecampana 3 ปีที่แล้ว

      @@ShiranAfergan Just sent you a Linkedin connection request, I hadn't seen that you said I could do it, Thank you Shiran!

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

    nice video! could i ask what tool you are using to draw on your screen?

    • @ShiranAfergan
      @ShiranAfergan  3 ปีที่แล้ว

      Thanks :) it’s an ipad pro and the app is GoodNotes

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

    I just looooove your lecture~!

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

    Thanks for the solution, Could you please make a video on 552. Student Attendance Record II problem?

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

      Thanks for the suggestion! I’ll look into it

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

    May I know why you are storing input in the address variables first?

    • @ShiranAfergan
      @ShiranAfergan  3 ปีที่แล้ว

      Hey Sai, which address variables? Which line of code are you referring to specifically?

  • @RahulSingh-vv9jj
    @RahulSingh-vv9jj 3 ปีที่แล้ว +2

    I am new to coding can you explain line no 6 , UnionFind(int n): groupcount(n){ /code here}, I didn't understand the ->:groupcount(n) , how we are using constructor.

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

      Hey Rahul, this line initializes the groupCount variable to n.

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

    UnionFind(int n):groupCount(n) {} can any please explain what this part signifies?
    i know constructors but i dont understand why colon and groupCount variable given after constructor

    • @ShiranAfergan
      @ShiranAfergan  3 ปีที่แล้ว

      This type of syntax is used to initialize the data members of a class. That specific line initializes “groupCount” to n. You can look up “CPP Initializer List” for more information.

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

      @@ShiranAfergan oh ok thank you so much

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

    Please make a video on cheapest flight within k stops

    • @ShiranAfergan
      @ShiranAfergan  3 ปีที่แล้ว

      Thanks for the suggestion! I’ll look into it :)

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

    c++ ? I wish it was in Java or C#
    but thanks for the preface it is amazing and clear

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

      Haha Cpp is my favorite language 🤓. Glad you liked the non coding parts 😆

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

    This problem is very implementation heavy

  • @yash.dwivedi
    @yash.dwivedi 3 ปีที่แล้ว +1

    which software do you use to write on the screen?

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

      It’s GoodNotes on an IPad pro

    • @yash.dwivedi
      @yash.dwivedi 3 ปีที่แล้ว

      @@ShiranAfergan Thank you for the answer

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

    Simply Awesome :)

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

    Leetcode 862 shortest subarray with sum atleast K if u get time please make video on it

    • @ShiranAfergan
      @ShiranAfergan  3 ปีที่แล้ว

      Thanks I’ll take a look :)

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

    good video! Help me a lot!

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

    Nice video Shiran :) You can also solve this using a graph with dfs (it's easier when you have forgotten how dsu works exactly 😂)

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

      Thanks Gabriel, good point! I also talked about it in the video but removed it cause it was too long 😄 i was saying that DFS will take longer because you have to construct the graph but will have similar complexity so it’s also a good solution

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

      @@ShiranAfergan Hi Shiran, you should check out this question : "Find Median from Data Stream". The solution is very interesting, also it seems to be a popular interview question this days :)

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

    Why you are not updating rank when either of merging group has lesser rank ?

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

    Impressive. Thanks

  • @RahulKashyap-yv5ox
    @RahulKashyap-yv5ox 2 ปีที่แล้ว

    Leetcode 1631
    Path With Minimum Effort:Mam can you make video on this question .

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

    Code for the solution?

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

    Mam kindly do leetcode 862

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

    Amazing !

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

    Genius!!

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

    thanks :)

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

    vgood

  • @prashantsahu6212
    @prashantsahu6212 3 ปีที่แล้ว

    Indians are everywhere

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

    this problem should be labeled hard ffs

  • @AyushRaj-pm1dz
    @AyushRaj-pm1dz 2 ปีที่แล้ว +1

    Same C++ Code :
    class UnionFind{
    private:
    vector parent;
    vector ranks;
    public:
    int groupCount;
    UnionFind(int n){
    groupCount = n;
    parent.resize(n);
    ranks.resize(n);
    }
    void intialize(int x){
    parent[x] = x;
    }
    int find(int x){
    if(x!=parent[x])
    parent[x] = find(parent[x]);
    return parent[x];
    }
    void unify(int x,int y){
    int rootX = find(x);
    int rootY = find(y);
    if(rootX==rootY) return;
    groupCount--;
    if(ranks[rootX]>ranks[rootY])
    parent[rootY] = rootX;
    else{
    parent[rootX] = rootY;
    if(ranks[rootX]==ranks[rootY])
    ranks[rootY]++;
    }
    }
    };
    class Solution {
    public:
    vector accountsMerge(vector& accounts) {
    int n = accounts.size();
    UnionFind uf(n);

    unordered_map mail2accountIdx;
    for(int i=0;isecond].push_back(email);
    }
    }

    for(int i=0;i