(算法分析Week16)Accounts Merge[Medium]
2017-12-23 13:08
387 查看
721. Accounts Merge[Medium]
题目来源Description
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:
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].
Solution
给了人名和他用的邮箱,一个人可能有多个邮箱,也可能重名。题目要求是,把同一个人的邮箱合并,当两个名字拥有相同的邮箱时,可以确定是同一个人。本来想用一个map存用户名和对应的邮箱地址,然后遍历,在map中找,如果邮箱地址有相交部分,就把剩下的都加上去。但是有一个问题,比如[a, c]、[b, e], [e, c]这样的顺序,[b, e]会被认为不是同一个人的邮箱,然后WA。其实可以重复这样的操作两次,就不会有遗漏,利用set排序,能过,但是复杂度还是太高了。
一个应该比较正统的做法:归组。具体的思路可以看这个链接
Complexity analysis
用了递归不好估计,但是这个递归可以转化成循环,所以复杂度应该是O(n²)
Code
*这是按照归组的思路写的class Solution { public: vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) { vector<vector<string>> result; unordered_map<string, string> root; unordered_map<string, string> owner; unordered_map<string, set<string>> m; int size = accounts.size(); for (auto account : accounts) { for (int i = 1; i < account.size(); ++i) { root[account[i]] = account[i]; owner[account[i]] = account[0]; } } for (auto account : accounts) { string p = find(account[1], root); for (int i = 2; i < account.size(); ++i) { root[find(account[i], root)] = p; } } for (auto account : accounts) { for (int i = 1; i < account.size(); ++i) { m[find(account[i], root)].insert(account[i]); } } for (auto a : m) { vector<string> v(a.second.begin(), a.second.end()); v.insert(v.begin(), owner[a.first]); result.push_back(v); } return result; } string find(string s, unordered_map<string, string>& root) { return root[s] == s ? s : find(root[s], root); } };
Result
相关文章推荐
- (算法分析Week16)Remove Nth Node From End of List[Medium]
- (算法分析Week16)132 Pattern[Medium]
- 算法分析与设计丨第四周丨LeetCode(8)——Binary Tree Level Order Traversal(Medium)
- (算法分析Week15)2 Keys Keyboard[Medium]
- (算法分析Week12)Unique Paths II[Medium]
- (算法分析Week1)Different Ways to Add Parentheses[Medium]
- 算法分析与设计课程06——542. 01 Matrix(Medium)
- (算法分析Week15)Best Time to Buy and Sell Stock with Cooldown[Medium]
- (算法分析Week10)ZigZag Conversion[Medium]
- 算法分析与设计丨第十三周丨LeetCode(17)——Coin Change(Medium)
- (算法分析Week2)3Sum[Medium]
- (算法分析Week3)Pow(x,n)[Medium]
- (算法分析Week11)Generate Parentheses[Medium]
- 算法分析与设计丨第一周丨LeetCode(2)——Different Ways to Add Parentheses(Medium)
- 算法分析与设计课程09——474. Ones and Zeroes(Medium)
- (算法分析Week7)Divide Two Integers[Medium]
- 算法分析与设计——leetcode刷题之Add Two Numbers(Medium)
- (算法分析Week10)Letter Combinations of a Phone Number[Medium]
- 算法分析与设计丨第四周丨LeetCode(7)——Find Bottom Left Tree Value(Medium)
- (算法分析Week6)Longest Palindromic Substring[Medium]