12
返回列表 发新帖
楼主: mren
跳转到指定楼层
上一主题 下一主题
收起左侧

uber 电面跪经

🔗
dalejin2010 2019-3-14 07:40:41 | 只看该作者
全局:
有没有什么restrictions吗?
linearly把map读一下,然后把value做为key放到新的map里面
回复

使用道具 举报

🔗
xuyang06 2019-3-14 11:58:18 | 只看该作者
全局:
多谢分享很有用 感觉就是建一个reverse map 然后做一遍loop
回复

使用道具 举报

全局:
建立无向图,寻找每个connected component和所包含的无重复uuid
回复

使用道具 举报

🔗
sjpl2011 2019-3-20 15:05:16 | 只看该作者
全局:
create keyMap and valueMap, loop them.
回复

使用道具 举报

🔗
LIYUJIE 2019-3-20 15:10:24 | 只看该作者
全局:
积分不到看不见,先码住。
回复

使用道具 举报

🔗
dionwang 2019-3-24 14:49:41 | 只看该作者
全局:
这个题越想越觉得不好做,有人有思路怎么做吗?
回复

使用道具 举报

🔗
Miaaaaa 2019-4-12 05:35:54 | 只看该作者
全局:
脑袋可能有点短路了,为什么不是简单的loop一遍<id, [phone numbers]>来reverse indexing一个<phone number, [ids]>?


补充内容 (2019-4-12 05:37):
ohhh, nvm,没看完题。。
回复

使用道具 举报

🔗
chenwoo 2019-4-13 13:03:59 | 只看该作者
全局:
Just my two cents: use union and find. We treat driver ID and number as the same for the sake of union and find. Later we distinguish them in the final result;


input:
Map<String, List<String>> map

output:
List<List<String>>, the numbers and uuids in one group will be in the same ArrayList. e.g. [[a, c, 1], [b, 2, 3, e], [d, 5]]

class UnionFind {
    Map<String, String> parents;
    public UnionFind() {
        parents = new HashMap<>();
    }

    public String find(String u) {

        while (!parents.get(u).equals(u) ) {
            String p1 = parents.get(u);
            String p2 = parents.get(p1);
            parents.put(u, p2);
            u = p1;
        }
        return u;
    }

    public void union(String u, String v) {
        String pu = find(u);
        String pv = find(v);
        if (pu.equals(pv)) {
            return;
        }
        // TODO, we can do union by rank here to flatten the union and find tree
        parents.put(pu, pv);
    }
}

List<List<String>> findRelationship(Map<String, List<String>> map) {
    UnionFind set = new UnionFind();
    for (String id : map.keySet()) {
        set.parents.put(id, id);
        List<String> numbers = map.get(id);

        // assume the 0th phone number is the root;
        String root = numers.get(0);
        for (String num : numbers) {
            set.parents.put(num, num);
            set.union(num, root);
        }
        set.union(id, root);
    }

    Map<String, List<String>> groups = new HashMap<>();
    for (String root : set.parents.keySet()) {
        gropups.computeIfAbsent(root, k -> new ArrayList<>()).add(set.parents.get(root));
    }

    // final result will be [[a, c, 1], [b, 2, 3, e], [d, 5]]
    // if we want to get the uuid -> phone number
    // we just need to a helper function to further process each ArrayList
    return new ArrayList<>(groups.entrySet());
}



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表