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

挨骂总OA面经

🔗
klai 2017-9-13 10:32:32 | 只看该作者
全局:
多谢分享。
请问一下这题输入 (a,b), (c,b) 和 (a,b), (b,c) 的返回是一样的么(a, b, c)?
回复

使用道具 举报

🔗
wwei17 2017-9-22 04:21:00 | 只看该作者
全局:
你的union find的算法不太对 我跑了一下case {{“1”,“2”}, {“3”,“4”},{ “1“, ”3”}}, 其结果应该为 1234, 但是你的算法只得到123, 原因出在这里:
        for(Map.Entry<String, String> ent : unionKeys.entrySet()) {-google 1point3acres
            counts.put(ent.getValue(), counts.getOrDefault(ent.getValue(), 0) + 1);
        }
是不能直接加的 要重复寻找unionkeys的unionkeys的unionkeys的。。。直到key不变为止 具体算法见 https://www.cs.princeton.edu/~rs/AlgsDS07/01UnionFind.pdf
回复

使用道具 举报

🔗
waynetang 2017-9-23 04:17:27 | 只看该作者
全局:
据说遥遥领先 发表于 2017-8-24 11:31
请问什么叫做字典顺序最小的?每个朋友圈是一个字符串数组,对于字符串数组如何比较字典顺序?

我感觉他这个答案是当有两个朋友圈数量一样时,比较两个朋友圈里面的字典序最小的人的字典序,我觉得应该是这样
回复

使用道具 举报

🔗
waynetang 2017-9-23 10:01:35 | 只看该作者
全局:
自己写了个比较好理解的版本,供大家参考
static Map<String,String> pre = new HashMap<>();
    public static String[] findLargestFriendCircle(String[][] arr) {
        for(int i = 0; i < arr.length; i ++){
            String m = arr[i][0];
            pre.put(m, m);
            String n = arr[i][1];
            pre.put(n, n);
        }
        for(int i = 0; i < arr.length; i ++){
            String m = arr[i][0];
            String n = arr[i][1];
            String parM = get(m);
            String parN = get(n);
            if(parM.compareTo(parN) < 0){
                pre.put(parM, parN);
            }
            else{
                pre.put(parN, parM);
            }
        }
        Map<String, List<String>> map = new HashMap<>();
        for(String key : pre.keySet()){
            String root = get(key);
            if(map.containsKey(root)){
                map.get(root).add(key);
            }
            else{
                List<String> list = new ArrayList<>();
                list.add(key);
                map.put(root, list);
            }
        }
        int max = 0;
        String maxKey = "";
        for(String str : map.keySet()){
            if(map.get(str).size() > max || map.get(str).size() == max && str.compareTo(maxKey) < 0){
                max = map.get(str).size();
                maxKey = str;
            }
        }
        List<String> list = map.get(maxKey);
        String[] result = new String[list.size()];
        for(int i = 0; i < list.size(); i ++){
            result[i] = list.get(i);
        }
        return result;
    }
    public static String get(String name){
        String root = name;
        while(pre.get(root) != root){
            root = pre.get(root);
        }
        while(pre.get(name) != root){
            String par = pre.get(name);
            pre.put(name, root);
            name = par;
        }
        return root;
    }
回复

使用道具 举报

🔗
iceman 2017-10-29 12:28:00 | 只看该作者
全局:
顶12楼 - local run 加了这段就能过1234 case了:
        /************************************************************/
        // critical: get final root for each person
        // need this to ensure all people are mapped to "real" root
        for(Map.Entry<String, String> ent : pre.entrySet()) {
                String curVal = ent.getValue();
                String curKey = ent.getKey();
                if (root.containsKey(curVal)) {
                        String finalRoot = find(root, curVal);
                        pre.put(curKey, finalRoot);
                }
        }
        /************************************************************/
回复

使用道具 举报

🔗
zhxymacau2017 2018-1-24 04:42:20 | 只看该作者
全局:
waynetang 发表于 2017-9-23 10:01
自己写了个比较好理解的版本,供大家参考
static Map pre = new HashMap();
    public static String[]  ...

谢谢你的solution,  很有启发
回复

使用道具 举报

🔗
tabletenniser 2018-1-24 14:43:43 | 只看该作者
全局:
不可以给图转成adjacency list之后给所有friend加到一个untraversed set里面,完了一个一个拿出来进行dfs来找出来每个朋友圈的size么? 之后dfs每traverse到一个friend就给remove from那个untraversed set?
回复

使用道具 举报

🔗
zzgzzm 2018-2-3 03:05:35 | 只看该作者
全局:
My solution. Ne sure to compress path in union-find. I also used the smallest person's name as representative of a circle.
  1. typedef vector<string> VS;
  2. unordered_map<string, string> rep; // representative of a circle

  3. string Find(string s) { // find circle representative for s
  4.     if (!rep.count(s)) rep[s] = s;
  5.     else if (rep[s] != s) rep[s] = Find(rep[s]);
  6.     return rep[s];
  7. }

  8. void Union(VS& p) { // join cicles of p[0] and p[1]
  9.     string rep1(Find(p[0])), rep2(Find(p[1]));
  10.     (rep1 < rep2)? rep[rep2] = rep1 : rep[rep1] = rep2;
  11. }

  12. VS largestCircle(vector<VS>& pairs) {   
  13.     for (auto& p : pairs) Union(p);
  14.     unordered_map<string, VS> circles;
  15.     for (auto& p : rep) circles[p.second].push_back(p.first);
  16.    
  17.     int maxCircle = 0;
  18.     string maxRep;
  19.     for (auto& p : circles)
  20.         if (p.second.size() > maxCircle ||
  21.            p.second.size() == maxCircle && p.first < maxRep)
  22.             maxCircle = p.second.size(), maxRep = p.first;
  23.     return circles[maxRep];
  24. }
复制代码
回复

使用道具 举报

🔗
zzgzzm 2018-2-3 03:27:04 | 只看该作者
全局:
tabletenniser 发表于 2018-1-24 14:43
不可以给图转成adjacency list之后给所有friend加到一个untraversed set里面,完了一个一个拿出来进行dfs来 ...

嗯,我觉得这样也可以,类似于"count number of island"题,但同时要记录每个“island”的大小和最小的名字。
回复

使用道具 举报

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

本版积分规则

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