1. 程式人生 > >Leetcode 721: Accounts Merge

Leetcode 721: Accounts Merge

all elements output dict nbsp foreach sent sel rip

Given a list accounts, each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails representing emails of the account.

Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some email that is common to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name.

After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails in sorted order. The accounts themselves can be returned in any order.

Example 1:

Input: 
accounts = [["John", "[email protected]", "[email protected]"], ["John", "[email protected]"], ["John", "[email protected]", "[email protected]"], ["Mary", "[email protected]"]]
Output: [["John", ‘[email protected]‘, ‘[email protected]‘, ‘[email protected]‘],  ["John", "[email protected]"], ["Mary", "[email protected]"]]
Explanation: 
The first and third John‘s are the same person as they have the common email "[email protected]".
The second John and Mary are different people as none of their email addresses are used by other accounts.
We could return these lists in any order, for example the answer [[‘Mary‘, ‘[email protected]‘], [‘John‘, ‘[email protected]‘], 
[‘John‘, ‘[email protected]‘, ‘[email protected]‘, ‘[email protected]‘]] would still be accepted.

Note:

  • The length of accounts will be in the range [1, 1000].
  • The length of accounts[i] will be in the range [1, 10].
  • The length of accounts[i][j] will be in the range [1, 30].

 1 public class Solution {
 2     public IList<IList<string>> AccountsMerge(IList<IList<string>> accounts) {
3 var result = new List<IList<string>>(); 4 5 if (accounts == null || accounts.Count == 0) return result; 6 7 // email address to list of accounts 8 var dict = new Dictionary<string, HashSet<int>>(); 9 10 // build the graph 11 for (int j = 0; j < accounts.Count; j++) 12 { 13 var account = accounts[j]; 14 for (int i = 1; i < account.Count; i++) 15 { 16 if (!dict.ContainsKey(account[i])) 17 { 18 dict[account[i]] = new HashSet<int>(); 19 } 20 21 dict[account[i]].Add(j); 22 } 23 } 24 25 var visited = new HashSet<string>(); 26 27 foreach (var email in dict.Keys) 28 { 29 if (visited.Contains(email)) continue; 30 visited.Add(email); 31 32 var list = new List<string>(); 33 list.Add(email); 34 35 var name = ""; 36 var queue = new Queue<string>(); 37 38 queue.Enqueue(email); 39 40 while (queue.Count > 0) 41 { 42 var e = queue.Dequeue(); 43 44 foreach (var aId in dict[e]) 45 { 46 var account = accounts[aId]; 47 name = account[0]; 48 49 for (int i = 1; i < account.Count; i++) 50 { 51 if (!visited.Contains(account[i])) 52 { 53 queue.Enqueue(account[i]); 54 list.Add(account[i]); 55 visited.Add(account[i]); 56 } 57 } 58 } 59 } 60 61 list.Insert(0, name); 62 63 // c# string comparer has different behavior than Java 64 list.Sort((Comparison<String>) ( 65 (String left, String right) => { 66 return String.CompareOrdinal(left, right); 67 } 68 )); 69 70 result.Add(list); 71 } 72 73 return result; 74 } 75 }

Leetcode 721: Accounts Merge