📣 4th of July限时特惠: VIP通行证立减$68
回复: 13
跳转到指定楼层
上一主题 下一主题
收起左侧

Youtube/Google 11/23电面

全局:

2015(10-12月) 码农类General 硕士 全职@google - 内推 - 技术电面  | | Other | 应届毕业生

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

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

x
挺nice的一个中国大姐,在google干了7年了~上来什么也没问直接做
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
*的距离最短

求onsite啊!!!

上一篇:关于Amazon OA2 invitation的疑问
下一篇:想发帖问下 Amazon OA 关于用C++的问题
全局:
写了下代码,有个问题是当计算从*到其他地方的位置时,可以经过'*'的吗?还是需要绕开?代码为可以经过的情况
  1. public class GymAndWall {

  2.         public static void main(String[] args) {
  3.                 GymAndWall gaw = new GymAndWall();
  4.                 char[][] matrix =  {{' ', ' ', ' ', ' '},
  5.                                                         {' ', '*', ' ', ' '},
  6.                                                         {' ', ' ', '*', '#'},
  7.                                                         {' ', '#', '*', ' '},};
  8.                 int[] res = gaw.findShortestDistance(matrix);
  9.                 System.out.println(res[0]);
  10.                 System.out.println(res[1]);
  11.                 // 1 2
  12.         }
  13.        
  14.         public int[] findShortestDistance(char[][] matrix) {
  15.                 int[] res = new int[2];
  16.                 res[0] = -1;
  17.                 res[1] = -1;
  18.                 if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
  19.                         return res;
  20.                 }
  21.                 int m = matrix.length;
  22.                 int n = matrix[0].length;
  23.                 int[][] dis = new int[m][n];
  24.                 boolean[][] visited = new boolean[m][n];
  25.                 for (int i = 0; i < m; i++) {
  26.                         for (int j = 0; j < n; j++) {
  27.                                 for (boolean[] row : visited) {
  28.                                         Arrays.fill(row, false);
  29.                                 }
  30.                                 if (matrix[i][j] == '*') {
  31.                                         bfs(matrix, i, j, dis, visited);
  32.                                 }
  33.                         }
  34.                 }
  35.                 int min = Integer.MAX_VALUE;
  36.                 for (int i = 0; i < m; i++) {
  37.                         for (int j = 0; j < n; j++) {
  38.                                 if (matrix[i][j] == ' ') {
  39.                                         if (dis[i][j] < min) {
  40.                                                 min = dis[i][j];
  41.                                                 res[0] = i;
  42.                                                 res[1] = j;
  43.                                         }
  44.                                 }
  45.                         }
  46.                 }
  47.                 for (int[] arr : dis) {
  48.                         for (int i : arr) {
  49.                                 System.out.print(i + " ");
  50.                         }
  51.                         System.out.println();
  52.                 }
  53.                
  54.                 return res;
  55.         }

  56.         private int[] dx = {1, -1, 0, 0};
  57.         private int[] dy = {0, 0, 1, -1};
  58.         private void bfs(char[][] matrix, int i, int j, int[][] dis, boolean[][] visited) {
  59.                 if (i < 0 || i >= matrix[0].length || j < 0 || j >= matrix[0].length || matrix[i][j] == '#' || visited[i][j]) {
  60.                         return;
  61.                 }
  62.                 Queue<Integer> queue = new LinkedList<Integer>();
  63.                 int len = matrix[0].length;
  64.                 int code = i * len + j;
  65.                 visited[i][j] = true;
  66.                 queue.offer(code);
  67.                 int step = 0;
  68.                 while (!queue.isEmpty()) {
  69.                         int size = queue.size();
  70.                         for (int k = 0; k < size; k++) {
  71.                                 code = queue.poll();
  72.                                 int x = code / len;
  73.                                 int y = code % len;
  74. //                                System.out.println(x + " " + y);
  75.                                 dis[x][y] += step;
  76.                                 for (int index = 0; index < dx.length; index++) {
  77.                                         int nx = x + dx[index];
  78.                                         int ny = y + dy[index];
  79.                                         if (nx >= 0 && nx < matrix.length && ny >= 0 && ny < matrix[0].length && !visited[nx][ny] && matrix[i][j] != '#') {
  80.                                                 queue.offer(nx * len + ny);
  81.                                                 visited[nx][ny] = true;
  82.                                         }
  83.                                 }
  84.                         }
  85.                         step++;
  86.                 }
  87.         }
  88. }
复制代码

补充内容 (2015-12-2 23:47):
dfs应该不行吧,步数没法控制
回复

使用道具 举报

推荐
litJordan 2015-12-3 03:01:35 | 只看该作者
全局:
自己也写了个。就是对每个‘*’进行BFS!最后把distance累加起来。
  1. /*

  2. '#' stands for obstacle, ' ' is path, and '*' is target. Get one cell in the matrix except obstacles which
  3.         has the shorest distance to all the targets.

  4.         idea: do BFS from each target and obtain the distance to all the other cells. Culmulate the sum of
  5.         distance to an overall dis matrix. The least sum would be the shortest distance cell.

  6.         Time: O(k * mn) where mn is the cell numbers and k is number of targets.

  7. */
  8. public class getAllShorestDist {
  9.         sprivate static final int[][] DIRECTIONS = new int[][] { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };

  10.         public static void main(String[] args) {

  11.                 char[][] matrix =  {{' ', '#', '*', ' '},
  12.                                         {' ', ' ', ' ', '#'},
  13.                                         {' ', '#', ' ', '#'},
  14.                                         {'*', '#', ' ', ' '}};
  15.                 getShortestDist(matrix);
  16.                
  17.         }

  18.         public static void getShortestDist(char[][] matrix) {
  19.                 if(matrix == null || matrix.length == 0 || matrix[0].length == 0)
  20.                         return;
  21.                
  22.                 int m = matrix.length;
  23.                 int n = matrix[0].length;
  24.                 int[][] dis = new int[m][n];
  25.                 boolean[][] visited = new boolean[m][n];
  26.                 for(int i = 0; i < m; i++) {
  27.                         for(int j = 0; j < n; j++) {
  28.                                 for(boolean[] row : visited)
  29.                                         Arrays.fill(row, false);
  30.                                 if(matrix[i][j] == '*') {
  31.                                         int[][] curDis = new int[m][n];
  32.                                         bfs(matrix, i, j, curDis, visited);
  33.                                         sumDistance(dis, curDis, matrix);
  34.                                 }
  35.                         }
  36.                 }
  37.                
  38.                 for(int k = 0; k < m; k++) {
  39.                         System.out.println(Arrays.toString(dis[k]));
  40.                 }
  41.         }

  42.         private static void bfs(char[][] matrix, int i, int j, int[][] curDis, boolean[][] visited) {
  43.                 Queue<int[]> queue = new LinkedList<int[]>();
  44.                 queue.offer(new int[] { i, j });
  45.                 visited[i][j] = true;
  46.                
  47.                 while(!queue.isEmpty()) {
  48.                         int[] point = queue.poll();
  49.                         int row = point[0];
  50.                         int col = point[1];
  51.                         for(int[] direction : DIRECTIONS) {
  52.                                 int r = row + direction[0];
  53.                                 int c = col + direction[1];
  54.                                 if(r < 0 || r >= matrix.length || c < 0 || c >= matrix[0].length ||
  55.                                                 visited[r][c] || matrix[r][c] == '#')
  56.                                         continue;
  57.                                 curDis[r][c] = curDis[row][col] + 1;
  58.                                 visited[r][c] = true;
  59.                                 queue.offer(new int[] { r, c });
  60.                         }
  61.                 }
  62.         }

  63.         public static void sumDistance(int[][] dis, int[][] curDis, char[][] matrix) {
  64.                 for(int i = 0; i < curDis.length; i++) {
  65.                         for(int j = 0; j < curDis[0].length; j++) {
  66.                                 if(matrix[i][j] != '#')
  67.                                         dis[i][j] += curDis[i][j];
  68.                         }
  69.                 }
  70.         }
  71. }
复制代码
回复

使用道具 举报

推荐
 楼主| xiaoniuona 2015-12-2 07:21:12 | 只看该作者
全局:
ssross 发表于 2015-12-1 10:53
请问一下LC 那道题不是只能求每个非wall的cell到一个最近的gate的距离吗?怎么样在bfs的时候求一个cell到 ...

从每个gate开始bfs,到一个cell的时候,update的不是更小的距离,而是把这次的距离累加上去。等到所有bfs结束,每个cell上就是到所有gate的距离的和了
回复

使用道具 举报

🔗
queeniejing 2015-12-1 02:29:48 | 只看该作者
全局:
谢谢LZ 分享, 请问你的第二题是 用BFS 做吗
回复

使用道具 举报

🔗
hyliu0000 2015-12-1 04:39:08 | 只看该作者
全局:
同问第二题。  目测是建立二维数组记录, dfs or bfs, 然后遍历数组得到最小
回复

使用道具 举报

🔗
queqichao 2015-12-1 05:34:26 | 只看该作者
全局:
第二题,建立一个2d table of int,然后从每一个健身器材开始BFS,对每个非wall cell对应的table里面cell累加到起始健身器材的距离。然后扫一遍table里面所有cell,找到距离之和最小的一个。
回复

使用道具 举报

🔗
 楼主| xiaoniuona 2015-12-1 05:51:02 | 只看该作者
全局:
queeniejing 发表于 2015-12-1 02:29
谢谢LZ 分享, 请问你的第二题是 用BFS 做吗

是的,参考LC那道walls and gates,这道题就是每次bfs不是找最小距离,而是把距离加起来
回复

使用道具 举报

🔗
 楼主| xiaoniuona 2015-12-1 05:51:48 | 只看该作者
全局:
hyliu0000 发表于 2015-12-1 04:39
同问第二题。  目测是建立二维数组记录, dfs or bfs, 然后遍历数组得到最小

bfs,我和3楼做法差不多
回复

使用道具 举报

🔗
litJordan 2015-12-1 10:53:20 | 只看该作者
全局:
xiaoniuona 发表于 2015-12-1 05:51
是的,参考LC那道walls and gates,这道题就是每次bfs不是找最小距离,而是把距离加起来

请问一下LC 那道题不是只能求每个非wall的cell到一个最近的gate的距离吗?怎么样在bfs的时候求一个cell到所有gate的累加的距离的和?
回复

使用道具 举报

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

本版积分规则

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