[路飞]_leetcode-721-账户合并

152 阅读2分钟

题目描述

[题目地址]

给定一个列表 accounts,每个元素 accounts[i] 是一个字符串列表,其中第一个元素 accounts[i][0] 是 名称 (name) ,其余元素是 emails 表示该账户的邮箱地址。

现在,我们想合并这些账户。如果两个账户都有一些共同的邮箱地址,则两个账户必定属于同一个人。请注意,即使两个账户具有相同的名称,它们也可能属于不同的人,因为人们可能具有相同的名称。一个人最初可以拥有任意数量的账户,但其所有账户都具有相同的名称。

合并账户后,按以下格式返回账户:每个账户的第一个元素是名称,其余元素是 按字符 ASCII 顺序排列 的邮箱地址。账户本身可以以 任意顺序 返回。

示例 1:

输入: accounts = [["John", "johnsmith@mail.com", "john00@mail.com"], ["John", "johnnybravo@mail.com"], ["John", "johnsmith@mail.com", "john_newyork@mail.com"], ["Mary", "mary@mail.com"]]
输出: [["John", 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com'],  ["John", "johnnybravo@mail.com"], ["Mary", "mary@mail.com"]]
解释:
第一个和第三个 John 是同一个人,因为他们有共同的邮箱地址 "johnsmith@mail.com"。 
第二个 JohnMary 是不同的人,因为他们的邮箱地址没有被其他帐户使用。
可以以任何顺序返回这些列表,例如答案 [['Mary''mary@mail.com']['John''johnnybravo@mail.com']['John''john00@mail.com''john_newyork@mail.com''johnsmith@mail.com']] 也是正确的。

示例 2:

输入: accounts = [["Gabe","Gabe0@m.co","Gabe3@m.co","Gabe1@m.co"],["Kevin","Kevin3@m.co","Kevin5@m.co","Kevin0@m.co"],["Ethan","Ethan5@m.co","Ethan4@m.co","Ethan0@m.co"],["Hanzo","Hanzo3@m.co","Hanzo1@m.co","Hanzo0@m.co"],["Fern","Fern5@m.co","Fern1@m.co","Fern0@m.co"]]
输出: [["Ethan","Ethan0@m.co","Ethan4@m.co","Ethan5@m.co"],["Gabe","Gabe0@m.co","Gabe1@m.co","Gabe3@m.co"],["Hanzo","Hanzo0@m.co","Hanzo1@m.co","Hanzo3@m.co"],["Kevin","Kevin0@m.co","Kevin3@m.co","Kevin5@m.co"],["Fern","Fern0@m.co","Fern1@m.co","Fern5@m.co"]]

提示:

  • 1 <= accounts.length <= 1000
  • 2 <= accounts[i].length <= 10
  • 1 <= accounts[i][j].length <= 30
  • accounts[i][0] 由英文字母组成
  • accounts[i][j] (for j > 0) 是有效的邮箱地址

解题思路

本题首先可以将同一个账户下的邮箱放到一个集合中,因为它们肯定是属于当前用户的。
然后在合并当前用户下的邮箱的过程中,同时判断该邮箱是否在其他集合存在,如果在其他集合存在,则说明两个集合属于同一个用户,则对两个集合进行合并。
最后根据获取到的集合,以及邮箱对应的用户名,组装结果数组即可。
那涉及到频繁的集合合并以及获取元素所在的集合的操作,我们可以利用并查集来解题。\

演示

q1.png

q2.png

q3.png

代码实现

//  1.创建2个map 一个是邮箱加坐标,一个是邮箱加名称
var accountsMerge = function(accounts) {
    const emailToIndex = new Map();//邮箱+坐标
    const emailToName = new Map();//邮箱+名称
    let emailsCount = 0;//初始化连通分量,邮箱里面第几个出现的

    for(const account of accounts){
        const name = account[0];//拿到每一个子数组名称
        const size = account.length;
        for(let i = 1; i < size; i++){//从第一个开始拿
            const email = account[i];//拿到邮箱
            if(!emailToIndex.has(email)){//开始判断邮箱地址有没有放到map里,通过邮箱找坐标
                emailToIndex.set(email,emailsCount++)//存坐标
                emailToName.set(email,name)//存名称
            }
        }
    }

    // 自此,就把两个 邮箱+名称和邮箱+坐标Map表创建完;
    // 拿到邮箱数量,创建并查集
    const uf = new UnionFind(emailsCount);
    // 根据两个map 进行合并
    for(const account of accounts){
        const firstEmail = account[1];//拿到第一个邮箱
        const firstEmailIndex = emailToIndex.get(firstEmail);//拿到它在Map里面的下标
        const size = account.length;
        for(let i = 1; i<size; i++){
            const nextEmail = account[i];//拿到后面的邮箱;
            const nextEmailIndex = emailToIndex.get(nextEmail);
            uf.unite(firstEmailIndex,nextEmailIndex);//开始合并两个邮箱
        }
    }

    const indexToEmails = new Map();//K是并查集祖先坐标,V是所有邮箱的集合;
    // 如果坐标当做k,如何拿到k emailToIndex
    for(const email of emailToIndex.keys()){
        const index = uf.findSet(emailToIndex.get(email));//拿到祖先坐标
        const account = indexToEmails.get(index) ? indexToEmails.get(index) : [];//拿到坐标
        account.push(email);//把邮箱添加完
        indexToEmails.set(index,account);//根据祖先节点分好组
    }

    // 做用户之间的合并
    const merged = [];
    //获取邮箱集合,合并之前要按ACSII码进行排序
    for(const emails of indexToEmails.values()){ 
        emails.sort();
        const name = emailToName.get(emails[0]);//拿到名称
        const account = [];
        account.push(name);
        account.push(...emails);
        merged.push(account);//把数组集合添加进去
    }
    return merged;//完成了用户合并;
};


class UnionFind{
    constructor(n){
        this.parent = new Array(n).fill(0).map((val,index)=>index);
        this.rank = new Array(n).fill(0);
        this.setCount = n;
    }

    findSet(index){
        if(this.parent[index] != index){
            this.parent[index] = this.findSet(this.parent[index])
        }
        return this.parent[index];
    }
    unite(index1,index2){
        let root1 = this.findSet(index1);
        let root2 = this.findSet(index2);
        if(root1 != root2){
            if(root1 < root2){
                [root1,root2] = [root2,root1];
            }
            this.parent[root2] = root1;
            this.rank[root1] += this.rank[root2];
            this.setCount--;
        }
    }
    getCount(){
         return this.setCount()
    }
    connected(index1,index2){
        let root1 = this.findSet(index1),root2 = this.findSet(index2);
        return root1 == root2
    }
}

至此我们就完成了leetcode-721-账户合并