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", "johnsmith@mail.com", "john00@mail.com"], ["John", "johnnybravo@mail.com"], ["John", "johnsmith@mail.com", "john_newyork@mail.com"], ["Mary", "mary@mail.com"]] Output: [["John", 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com'], ["John", "johnnybravo@mail.com"], ["Mary", "mary@mail.com"]] Explanation: The first and third John's are the same person as they have the common email "johnsmith@mail.com". 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', 'mary@mail.com'], ['John', 'johnnybravo@mail.com'], ['John', 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com']] would still be accepted.
Note:
accounts
will be in the range [1, 1000]
.accounts[i]
will be in the range [1, 10]
.accounts[i][j]
will be in the range [1, 30]
.Union Find
最后返回的时候繁琐了一点,因为题目要求sort过,跟名字一定要在第一位。
Todo:分析复杂度
class Solution { public List<List<String>> accountsMerge(List<List<String>> accounts) { Map<String, Integer> m = new HashMap<>(); int n = accounts.size(); int[] uf = getUF(n); for (int i = 0; i < accounts.size(); i++) { List<String> row = accounts.get(i); for (int j = 1; j < row.size(); j++) { String email = row.get(j); if (m.containsKey(email) && getRoot(uf, m.get(email)) != getRoot(uf, i)) { int index = m.get(email); uf[getRoot(uf, uf[i])] = getRoot(uf, index); n--; }else { m.put(email, getRoot(uf, i)); } } } return sortAndReturn(m, uf); } private List<List<String>> sortAndReturn(Map<String, Integer> m, int[] uf) { Map<Integer,List<String>> rt = new HashMap<>(); for (Map.Entry<String, Integer> entry : m.entrySet()) { int index = getRoot(uf, entry.getValue()); if (!rt.containsKey(index)) { rt.put(index, new ArrayList<String>()); } rt.get(index).add(entry.getKey()); } List<List<String>> ret= new ArrayList<>(); for (List<String> l : rt.values()) { List<String> temp = new ArrayList<>(l); Collections.sort(temp); String name = accounts.get(getRoot(uf, m.get(temp.get(0)))).get(0); temp.add(0, name); ret.add(temp); } return ret; } private int getRoot(int[] uf, int index) { while (uf[index] != index) { index = uf[index]; uf[index] = uf[uf[index]]; } return index; } private int[] getUF(int n) { int[] rt = new int[n]; for (int i = 0; i < n; i++) { rt[i] = i; } return rt; } }
ToDo, DFS做法,见https://leetcode.com/problems/accounts-merge/solution/
No comments:
Post a Comment