📣 Back to School开学季 - VIP通行证5折优惠!蓝莓、Offer多多同步优惠
楼主: 匿名
跳转到指定楼层
上一主题 下一主题
收起左侧

记一次愉悦的Google Onsite体验

 
地里匿名用户
🔗
匿名用户-HAPEK  2019-11-14 04:02:45
”说实话,我有那么一瞬间我是不愉悦的 我觉得小哥剥夺了我思考乐趣,之后整个过程就变得索然无味了起来.....“ 能说出这句话的都是真大神
回复

使用道具 举报

🔗
xiaobai123 2019-11-14 10:12:01 | 只看该作者
全局:
Can you give an example of this statement? Thanks a lot. "follow up 好像是小哥拍脑袋想的 说那怎么找包含targe的最大环呢,我:?????? , 随便说了句如果最大环上有个小环 岂不是可以无限长, 无限大?"
回复

使用道具 举报

🔗
allenlipeng47 2019-11-14 13:30:25 | 只看该作者
全局:
本帖最后由 allenlipeng47 于 2019-11-14 13:34 编辑

第4题写了一下
  1. public class AntCrossRoad {

  2.     static class Segment {
  3.         double x;
  4.         double top;
  5.         double bottom;
  6.         boolean add;
  7.         public Segment(double x, double top, double bottom, boolean add) {
  8.             this.x = x;
  9.             this.top = top;
  10.             this.bottom = bottom;
  11.             this.add = add;
  12.         }
  13.     }

  14.     public static boolean walkable(double[][] squares) {
  15.         List<Segment> list = new ArrayList<>();
  16.         for (double[] square : squares) {
  17.             list.add(new Segment(square[0], square[1], square[3], true));
  18.             list.add(new Segment(square[2], square[1], square[3], false));
  19.         }
  20.         Collections.sort(list, (s1, s2) -> Double.compare(s1.x, s2.x));
  21.         TreeMap<Double, Integer> tm = new TreeMap<>();
  22.         for (Segment segment : list) {
  23.             if (!isWalkable(tm)) {
  24.                 return false;
  25.             }
  26.             if (segment.add) {
  27.                 tm.put(segment.bottom, tm.getOrDefault(segment.bottom, 0) + 1);
  28.                 tm.put(segment.top, tm.getOrDefault(segment.top, 0) - 1);
  29.             } else {
  30.                 int bottom = tm.get(segment.bottom);
  31.                 if (bottom == 1) {
  32.                     tm.remove(segment.bottom);
  33.                 } else {
  34.                     tm.put(segment.bottom, bottom - 1);
  35.                 }
  36.                 int top = tm.get(segment.top);
  37.                 if (top == -1) {
  38.                     tm.remove(segment.top);
  39.                 } else {
  40.                     tm.put(segment.top, top + 1);
  41.                 }
  42.             }
  43.         }
  44.         return true;
  45.     }

  46.     private static boolean isWalkable(TreeMap<Double, Integer> tm) {
  47.         if (tm.isEmpty()) {
  48.             return true;
  49.         }
  50.         int layer = 0;
  51.         for (Map.Entry<Double, Integer> entry : tm.entrySet()) {
  52.             if (layer == 0 && entry.getKey() > 0 && entry.getKey() < 1) {
  53.                 return true;
  54.             }
  55.             layer += entry.getValue();
  56.         }
  57.         return tm.lastKey() < 1;
  58.     }

  59.     public static void main(String[] args) {
  60.         System.out.println(walkable(new double[][]{
  61.                 {0.2, 0.7, 0.3, 0.4}, {0.25, 0.5, 0.5, -0.1}
  62.         }));
  63.         System.out.println(walkable(new double[][]{
  64.                 {0.2, 0.7, 0.3, 0.4}, {0.25, 0.5, 0.5, -0.1}, {0.26, 1.1, 0.5, 0.6}
  65.         }));
  66.         System.out.println(walkable(new double[][]{
  67.                 {1, 0.5, 2, -1}, {3, 2, 5, 0.4}
  68.         }));
  69.         System.out.println(walkable(new double[][]{
  70.                 {1, 0.5, 2, -1}, {3, 2, 5, 0.4}, {4, 0.8, 6, -1}
  71.         }));
  72.     }
  73. }
复制代码


回复

使用道具 举报

🔗
allenlipeng47 2019-11-14 13:41:44 | 只看该作者
全局:
第1题,加followup
  1.     public int findMaxAward(int[] rev1, int[] rev2, int cost) {
  2.         int f1 = 0, f2 = 0;
  3.         for (int i = 0; i < rev1.length; i++) {
  4.             int f1Tmp = Math.max(f1, f2 - cost) + rev1[i];
  5.             int f2Tmp = Math.max(f2, f1 - cost) + rev2[i];
  6.             f1 = f1Tmp;
  7.             f2 = f2Tmp;
  8.         }
  9.         return Math.max(f1, f2);
  10.     }

  11.     public List<Integer> findMaxAwardWithList(int[] rev1, int[] rev2, int cost) {
  12.         int f1 = 0, f2 = 0;
  13.         int[][] track = new int[2][rev1.length];
  14.         Arrays.fill(track[0], -1);
  15.         Arrays.fill(track[1], -1);
  16.         for (int i = 0; i < rev1.length; i++) {
  17.             int f1Tmp = 0, f2Tmp = 0;
  18.             if (f1 >= f2 - cost) {
  19.                 f1Tmp = f1 + rev1[i];
  20.                 track[0][i] = 0;
  21.             } else {
  22.                 f1Tmp = f2 - cost + rev1[i];
  23.                 track[0][i] = 1;
  24.             }
  25.             if (f2 >= f1 - cost) {
  26.                 f2Tmp = f2 + rev2[i];
  27.                 track[1][i] = 1;
  28.             } else {
  29.                 f2Tmp = f1 - cost + rev2[i];
  30.                 track[1][i] = 0;
  31.             }
  32.             f1 = f1Tmp;
  33.             f2 = f2Tmp;
  34.         }
  35.         LinkedList<Integer> list = new LinkedList<>();
  36.         int curr = f1 >= f2 ? 0 : 1;
  37.         for (int i = rev1.length - 1; i >= 0; i--) {
  38.             list.addFirst(curr);
  39.             curr = track[curr][i];
  40.         }
  41.         return list;
  42.     }

  43.     public static void main(String[] args) {
  44.         Rev1Rev2 rev1Rev2 = new Rev1Rev2();
  45.         System.out.println(rev1Rev2.findMaxAwardWithList(new int[]{1, 3}, new int[]{3, 1}, 3));
  46.         System.out.println(rev1Rev2.findMaxAwardWithList(new int[]{1, 3}, new int[]{3, 1}, 1));
  47.         System.out.println(rev1Rev2.findMaxAwardWithList(new int[]{1, 30, 2}, new int[]{10, 1, 5}, 3));
  48.         System.out.println(rev1Rev2.findMaxAwardWithList(new int[]{1, 5, 2}, new int[]{3, 1, 5}, 1));
  49.     }
复制代码
回复

使用道具 举报

🔗
allenlipeng47 2019-11-14 13:43:10 | 只看该作者
全局:
本帖最后由 allenlipeng47 于 2019-11-14 13:45 编辑

3.a
  1. public class FindTopKInBst {

  2.     public static class TreeNode {
  3.         int val;
  4.         int count;
  5.         TreeNode left;
  6.         TreeNode right;
  7.         TreeNode(int val, int count) {
  8.             this.val = val;
  9.             this.count = count;
  10.         }
  11.     }

  12.     public static TreeNode findTopKInBst(TreeNode node, int k) {
  13.         if (node == null || node.count < k) {
  14.             return null;
  15.         }
  16.         if (node.right == null) {
  17.             if (node.count == 1) {
  18.                 return node;
  19.             } else {
  20.                 return findTopKInBst(node.left, k - 1);
  21.             }
  22.         } else {
  23.             if (node.right.count == k - 1) {
  24.                 return node;
  25.             } else if (node.right.count >= k) {
  26.                 return findTopKInBst(node.right, k);
  27.             } else {
  28.                 return findTopKInBst(node.left, k - node.right.count - 1);
  29.             }
  30.         }
  31.     }

  32.     public static void main(String[] args) {
  33.         TreeNode t4 = new TreeNode(4, 7);
  34.         TreeNode t2 = new TreeNode(2, 3);
  35.         TreeNode t1 = new TreeNode(1, 1);
  36.         TreeNode t3 = new TreeNode(3, 1);
  37.         TreeNode t6 = new TreeNode(6, 3);
  38.         TreeNode t5 = new TreeNode(5, 1);
  39.         TreeNode t7 = new TreeNode(7, 1);
  40.         t4.left = t2; t4.right = t6;
  41.         t2.left = t1; t2.right = t3;
  42.         t6.left = t5; t6.right = t7;
  43.         System.out.println(findTopKInBst(t4, 8));
  44.     }
  45. }
复制代码

回复

使用道具 举报

🔗
allenlipeng47 2019-11-14 13:43:56 | 只看该作者
全局:
3.b
  1. public class ShortestCircle {

  2.     public int findShortestCircle(int[][] edges, int N, int target) {
  3.         Map<Integer, List<Integer>> graph = new HashMap<>();
  4.         for (int[] edge : edges) {
  5.             List<Integer> neighbor = graph.getOrDefault(edge[0], new ArrayList<>());
  6.             neighbor.add(edge[1]);
  7.             graph.put(edge[0], neighbor);
  8.             graph.put(edge[1], graph.getOrDefault(edge[1], new ArrayList<>()));
  9.         }
  10.         int ans = 1;
  11.         LinkedList<Integer> queue = new LinkedList<>();
  12.         queue.addLast(target);
  13.         boolean[] visited = new boolean[N];
  14.         visited[target] = true;
  15.         while (!queue.isEmpty()) {
  16.             int size = queue.size();
  17.             for (int i = 0; i < size; i++) {
  18.                 int curr = queue.removeFirst();
  19.                 for (int next : graph.get(curr)) {
  20.                     if (next == target) {
  21.                         return ans;
  22.                     }
  23.                     if (visited[next]) {
  24.                         continue;
  25.                     }
  26.                     visited[next] = true;
  27.                     queue.addLast(next);
  28.                 }
  29.             }
  30.             ans++;
  31.         }
  32.         return -1;
  33.     }

  34.     public static void main(String[] args) {
  35.         ShortestCircle shortestCircle = new ShortestCircle();
  36.         System.out.println(shortestCircle.findShortestCircle(new int[][]{{0, 0}}, 1, 0));
  37.         System.out.println(shortestCircle.findShortestCircle(new int[][]{{0, 1}, {1, 2}}, 3, 0));
  38.         System.out.println(shortestCircle.findShortestCircle(new int[][]{{0, 1}, {1, 2}, {2, 0}}, 3, 0));
  39.         System.out.println(shortestCircle.findShortestCircle(new int[][]{{0, 1}, {1, 2}, {2, 3}, {2, 0}}, 4, 0));
  40.         System.out.println(shortestCircle.findShortestCircle(new int[][]{{0, 1}, {1, 2}, {2, 3}, {2, 0}, {3, 0}}, 4, 3));
  41.     }
  42. }
复制代码
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-3GMQR  2019-11-15 12:49:17
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
aegis_bing 2019-11-22 06:59:51 | 只看该作者
全局:
高手,刷半天也是晕的 望尘莫及

评分

参与人数 1大米 +1 收起 理由
besimple1024 + 1 欢迎来一亩三分地论坛!

查看全部评分

回复

使用道具 举报

🔗
aegis_bing 2019-11-22 14:39:29 | 只看该作者
全局:
fatalme 发表于 2019-11-13 15:33
嗯,leetcode上的变种。台湾哥出的没想象的那么难。

请问是哪道啊?茫茫题海lost了

评分

参与人数 1大米 +1 收起 理由
besimple1024 + 1 欢迎来一亩三分地论坛!

查看全部评分

回复

使用道具 举报

🔗
pandami 2019-11-22 15:04:53 来自APP | 只看该作者
全局:
室友问我为什么跪着看面筋系列
回复

使用道具 举报

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

本版积分规则

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