RainX_69

UNION FIND (MERGE ACCOUNTS) IMPORTANT

Jan 14th, 2023
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.96 KB | Source Code | 0 0
  1. THERE ARE TWO QUESTIONS, LEETCODE AND GFG. GFG ONE IS A BIT TRICKY, AND LEETCODE ONE IS SUPER EASY
  2. I WILL BE PASTING QUESTION FROM THE GFG. DO NOT WORRY, I WILL ATTACH LINKS TO BOTH SITE QUESTIONS
  3.  
  4. https://leetcode.com/problems/accounts-merge/
  5. https://practice.geeksforgeeks.org/problems/merging-details/1
  6.  
  7. ---------------------------------------------------------------------------------------------------------------------------------------
  8.  
  9. Bob, a teacher of St. Joseph School given a task by his principal to merge the details of the students where each element details[i] is a list of strings, where the first element details[i][0] is a name of the student, and the rest of the elements are emails representing emails of the student.   Two details definitely belong to the same student if there is some common email to both detail.  After merging the details, return the details of the student in the following format: the first element of each detail is the name of the student, and the rest of the elements are emails in sorted order. The details themselves can be returned in any order.  Note: Two details have the same name, they may belong to different people as people could have the same name. A person can have any number of details initially, but all of their details definitely have the same name.
  10. Note: In case 2 or more same email belongs to 2 or more different names merge with first name only. Print in the order in sorted way according to the name of the details.
  11.  
  12. Example 1:
  13. Input:
  14. n: 4
  15. details =
  16. ["Mary","[email protected]"],
  17. ["John","[email protected]"]]
  18. Output:
  19. ["John","[email protected]"]]
  20. Explanation:
  21. The first and second John's are the same person as
  22. they have the common email "[email protected]".
  23. The third John and Mary are different people as none
  24. of their email addresses are used by other accounts.
  25. We could return these lists in any order, for example
  26. the answer [['Mary', 'mary@mail.com'],
  27. ['John', 'johnnybravo@mail.com'],
  28. ['John', 'john00@mail.com', 'john_newyork@mail.com',
  29. 'johnsmith@mail.com']]
  30. would still be accepted.
  31.  
  32. Example 2:
  33. Input:
  34. n: 5
  35. details =
  36. Output:
  37. Explanation:
  38. We don't have any common emails in any of the users.
  39. We just sorted the emails of each person and we
  40. return a list of the emails.(The details can be
  41. returned in any order).
  42.  
  43.  
  44. --------------------------------------------------------------------------------------------------------------------------------------
  45.  
  46.  
  47. class DS{
  48. private:
  49.     int* parent;
  50.     int* rank;
  51.     int n;
  52. public:
  53.     DS(int n){
  54.         this->n=n;
  55.         parent=new int[n];
  56.         rank=new int[n];
  57.         for(int i=0;i<n;i++){
  58.             parent[i]=i;
  59.             rank[i]=0;
  60.         }
  61.     }
  62.    
  63.     int findParent(int node){
  64.         if(parent[node]==node){
  65.             return node;
  66.         }
  67.         return parent[node]=findParent(parent[node]);
  68.     }
  69.    
  70.     void merge(int PreviousPerson, int CurrentPerson){
  71.         int P_PP=findParent(PreviousPerson);
  72.         int P_CP=findParent(CurrentPerson);
  73.         if(P_PP==P_CP){
  74.             return;
  75.         }
  76.         if(rank[P_PP]>rank[P_CP]){
  77.             parent[P_CP]=P_PP;
  78.         }
  79.         else if(rank[P_PP]<rank[P_CP]){
  80.             parent[P_PP]=P_CP;
  81.         }
  82.         if(rank[P_PP]==rank[P_CP]){
  83.             /* THIS IS IMPORTANT...IF THERE ARE TWO SETS WITH EQUAL
  84.                 SIZE, YOU WANT THE CURRENT PERSON TO POINT TO PERVIOUS
  85.                 PERSON, AS PER QUESTION. IF TWO DIFFERENT NAME PERSONS
  86.                 HAVE SAME EMAIL, YOU WANT THE PERSON WHOSE INDEX WAS
  87.                 SMALLER TO KEEP HIS NAME */
  88.             parent[P_CP]=P_PP;
  89.             rank[P_PP]++;
  90.         }
  91.     }
  92. };
  93.  
  94. class Solution {
  95.   public:
  96.     vector<vector<string>> mergeDetails(vector<vector<string>>& details) {
  97.         int n=details.size();
  98.         DS ds(n);
  99.        
  100.         unordered_map<string,int> mpp;  // email to personINDEX
  101.         for(int personID=0;personID<n;personID++){
  102.             for(int emailID=1;emailID<details[personID].size();emailID++){
  103.                 string mail=details[personID][emailID];
  104.                 if(mpp.find(mail)==mpp.end()){
  105.                     mpp[mail]=personID;
  106.                 }
  107.                 else{
  108.                     ds.merge(mpp[mail],personID);
  109.                 }
  110.             }
  111.         }
  112.        
  113.         vector<string> mails[n];
  114.         for(auto itr: mpp){
  115.             int personID=itr.second;
  116.             string mail=itr.first;
  117.             int representative=ds.findParent(personID);
  118.             mails[representative].push_back(mail);
  119.         }
  120.        
  121.         vector<vector<string>> res;
  122.         for(int i=0;i<n;i++){
  123.             if(mails[i].size()==0){
  124.                 continue;
  125.             }
  126.             sort(mails[i].begin(),mails[i].end());
  127.             vector<string> detail;
  128.            
  129.             string name=details[i][0];
  130.             detail.push_back(name);
  131.             for(auto mail: mails[i]){
  132.                 detail.push_back(mail);
  133.             }
  134.             res.push_back(detail);
  135.         }
  136.        
  137.         sort(res.begin(),res.end(),greater<vector<string>>());  
  138.         return res;
  139.     }
  140. };
  141.  
  142. ---------------------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment