(算法分析Week16)Accounts Merge[Medium]
来源:互联网 发布:刘国梁怎么了知乎 编辑:程序博客网 时间:2024/06/06 02:49
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)Accounts Merge[Medium]
- (算法分析Week16)132 Pattern[Medium]
- (算法分析Week16)Remove Nth Node From End of List[Medium]
- Leetcode 738. Monotone Increasing Digits(算法分析week16)
- 721. Accounts Merge
- LeetCode 721( Accounts Merge)
- LeetCode 721. Accounts Merge
- 【算法设计作业】week16
- LWC 57:721. Accounts Merge
- (算法分析Week8)Triangle[Medium]
- week16
- (算法分析Week2)3Sum[Medium]
- (算法分析Week3)Pow(x,n)[Medium]
- (算法分析Week5)Add Two Numbers[Medium]
- (算法分析Week6)Longest Palindromic Substring[Medium]
- (算法分析Week7)Divide Two Integers[Medium]
- (算法分析Week10)ZigZag Conversion[Medium]
- (算法分析Week11)Generate Parentheses[Medium]
- 快捷键
- python 字典怎样当作参数传入函数里,以及在函数里的一些遍历。变量的作用域。
- 利用 boost 库,编写c++类 FilesDirectoryTree,获得某个文件夹下符合某种格式要求(pcd)的文件路径,并按照降序或升序排列
- java基础复习(数组)
- 《利用Python进行数据分析》第8章 绘图和可视化笔记
- (算法分析Week16)Accounts Merge[Medium]
- Hive转Oracle存储过程 查询字段包含子查询修改
- bzoj1452 [JSOI2009]Count
- 6-2 多态性与虚函数
- 【web】《javascript语言精粹》笔记(一)
- iPhoneX截图如何带“刘海”和圆角
- 蜘蛛不爬取,您检查过网站的robots了么
- bubble sort详解
- JAVA删除字符串中指定字符集(删除敏感字符)