📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
回复: 8
跳转到指定楼层
上一主题 下一主题
收起左侧

compass经典高频题 game rank解法

全局:

2019(7-9月) 码农类General 硕士 全职@compass - 内推 - Onsite  | | Pass | 在职跳槽

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 火烈猴19 于 2019-8-22 10:46 编辑

给一系列比赛结果,例如“a beats b”, “b beat c”,“c beat d”;输出最终排名,这个例子是abcd,算是alien dictionary的变形。
不存在circle,例如“d beats a”。然后问了time and space complexity,还有会用什么样的test case去测试。

先上代码

  1. public class GetGameRank {

  2.     public static void main(String[] args) {
  3.         // write your code here
  4.         String[] test1 = new String[]{"a beat b", "b beat c", "c beat e"};
  5.         getOrder(test1).forEach(x -> System.out.print(x + " ")); //a b c e
  6.         System.out.println();

  7.         String[] test2 = new String[]{"a beat b", "a beat c"};
  8.         getOrder(test2).forEach(x -> System.out.print(x + " ")); //a b c OR a c b
  9.         System.out.println();

  10.         System.out.println(getOrder(null).size() == 0);


  11.         String[] test3 = new String[]{"a beat b", "b beat c", "c beat e", "e beat f", "e beat d", "d beat k"}; // a b c e d k f
  12.         getOrder(test3).forEach(x -> System.out.print(x + " ")); //a b c e
  13.         System.out.println();
  14.     }

  15.     public static List<String> getOrder(String[] games) {
  16.         LinkedList<String> res = new LinkedList<>();
  17.         if (games == null || games.length == 0) return res;
  18.         Map<String, Integer> inDegree = new HashMap<>();
  19.         Map<String, List<String>> graph = new HashMap<>();
  20.         for (String game : games) {
  21.             String[] team = game.split(" beat ");
  22.             inDegree.put(team[1], inDegree.getOrDefault(team[1], 0));
  23.             inDegree.put(team[0], inDegree.getOrDefault(team[0], 0) + 1);
  24.             graph.computeIfAbsent(team[1], x -> new ArrayList<>()).add(team[0]);
  25.         }
  26.         Queue<String> queue = new LinkedList<>();
  27.         Iterator<Map.Entry<String, Integer>> iterator = inDegree.entrySet().iterator();
  28.         while (iterator.hasNext()) {
  29.             Map.Entry<String, Integer> entry = iterator.next();
  30.              if (entry.getValue() == 0) {
  31.                  queue.offer(entry.getKey());
  32.                  iterator.remove();
  33.              }
  34.         }

  35.         while (!queue.isEmpty()) {
  36.             String curr = queue.poll();
  37.             List<String> next = graph.get(curr);
  38.             res.addFirst(curr);
  39.             if (next == null || next.size() == 0) continue;
  40.             for (String n : next) {
  41.                 inDegree.put(n, inDegree.get(n) - 1);
  42.                 if (inDegree.get(n) == 0) {
  43.                     inDegree.remove(n);
  44.                     queue.offer(n);
  45.                 }
  46.             }
  47.         }
  48.         return res;
  49.     }
  50. }
复制代码


首先看到这种题 一个压着一个 第一个想到的就是topological sort (拓扑排序)

首先我们要将这个String 进行拆分

每个String的格式基本都是
xxxx beat yyyy
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
这里不是我为了展现代码风骚来装逼才使用 iterator. 因为使用for loop来循环这个map 然后遇到indegree为0的entry 然后remove掉这个entry会导致concurrentModificationException

Iterator.remove()不会抛出ConcurrentModificationException,因为这是在迭代时修改集合的允许方式.
面试的时候记得避坑!

  1.         Iterator<Map.Entry<String, Integer>> iterator = inDegree.entrySet().iterator();
  2.         while (iterator.hasNext()) {
  3.             Map.Entry<String, Integer> entry = iterator.next();
  4.              if (entry.getValue() == 0) {
  5.                  queue.offer(entry.getKey());
  6.                  iterator.remove();
  7.              }
  8.         }
复制代码


这个我就不仔讲了 详情大家感兴趣可以去网上搜搜看

评分

参与人数 2大米 +31 收起 理由
Ccqw12 + 1 给你点个赞!
匿名用户-D0KST + 30

查看全部评分


上一篇:狗狗家新鲜面经
下一篇:AppFolio丧气店面
🔗
wangzhuyun32 2019-8-24 05:38:11 | 只看该作者
本楼:
全局:
👍
回复

使用道具 举报

🔗
xgj-iwnl 2019-8-24 06:23:06 | 只看该作者
全局:
想问如果"a beat c" "b beat c", 最后a和b怎么排名?
回复

使用道具 举报

🔗
helloteacha 2019-9-6 11:22:34 | 只看该作者
全局:
同学,感谢分享代码和这么详细的解析。如果你把indegree的map和graphmap的key-value顺序颠倒的话,你可以直接res.add(cur)把cur添加到list的末尾了。
回复

使用道具 举报

全局:
哥们 我也马上去onsite 能再多说点onsite面试的事情吗
回复

使用道具 举报

🔗
路飞之父 2019-9-12 14:13:55 | 只看该作者
全局:
https://www.youtube.com/watch?v=ddTC4Zovtbc&t=542s
感觉这个方法简单些,先建图然后对图bfs就好了
回复

使用道具 举报

🔗
路飞之父 2019-9-12 14:14:40 | 只看该作者
全局:
说错了 应该是dfs
回复

使用道具 举报

🔗
goodluckoffer 2019-9-18 12:56:44 | 只看该作者
全局:
这个哪里是dfs? 典型的图的遍历 找到入读为0 的点 用queuebfs
回复

使用道具 举报

🔗
brian1868 2019-11-3 05:56:58 | 只看该作者
全局:
楼主这个是高频题?我都没在面经里看到过。。。
回复

使用道具 举报

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

本版积分规则

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