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

谷歌onsite新题

全局:
楼主给个timeliness吧。另外刷题刷了多久呢
回复

使用道具 举报

🔗
Rex_Coding 2018-11-24 11:47:26 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
Rex_Coding 2018-11-24 14:33:05 | 只看该作者
全局:
这是我的暴力解法,用union find减少可能的开始点,然后每个开始点都按照序列flip一遍看需要多少翻转多少次,然后取最小值。 时间和空间复杂度都是O(n^3),感觉不是很优,但是实在是想不出来了= =
  1. public class FlipColors {
  2.         @Test
  3.         public void test() {
  4.                 char[][] grid = new char[][] {
  5.                         {'r', 'g','g'},
  6.                         {'b', 'r', 'b'},
  7.                         {'b', 'r', 'g'}
  8.                 };
  9. //                char[][] grid = new char[][] {
  10. //                        {'r', 'g','b','g','b','g','r'}
  11. //                };
  12.                 /*
  13.                  * Asumption: the fliping order is fixed:
  14.                  * order = new char[] {'r', 'g', 'b'};
  15.                  * we can not flip r to b directly
  16.                  *
  17.                  * when we choose the start point, we can't change the point and flip other cells
  18.                  * m = rows, n = cols
  19.                  * Time: O((mn)^3)
  20.                  * Space: O((mn) ^ 3 )
  21.                  * */
  22.                 int res = findMinFlip(grid);
  23.                 System.out.println(res);
  24.         }
  25.         int[][] dirs = new int[][] {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  26.         private char[] order = new char[] {'r', 'g', 'b'};
  27.         /*
  28.          * Main Procedure
  29.          * */
  30.         public int findMinFlip(char[][] grid) {
  31.                 int rows = grid.length;
  32.                 if(rows == 0) return 0;
  33.                 int cols = grid[0].length;
  34.                 if(cols == 0) return 0;
  35.                 // init unions
  36.                 Union union = new Union(rows * cols);
  37.                 // union cells with same color, to compress the start points
  38.                 for (int i = 0 ; i < rows ; i++) {
  39.                         for (int j = 0 ; j < cols ; j++) {
  40.                                 for(int k = 0 ; k < dirs.length ; k++) {
  41.                                         int x = i + dirs[k][0];
  42.                                         int y = j + dirs[k][1];
  43.                                         if(isValidate(grid, x, y) && grid[i][j] == grid[x][y]) {
  44.                                                 union.union(i * cols + j , x * cols + y);
  45.                                         }
  46.                                 }
  47.                         }
  48.                 }
  49.                 // pick up all the potential start points
  50.                 List<Integer> starts = new ArrayList<>();
  51.                 for(int i = 0 ; i < union.ids.length ; i++) {
  52.                         if(union.ids[i] == i) starts.add(i);
  53.                 }
  54.                 int res = Integer.MAX_VALUE;
  55.                 for(Integer start : starts) {
  56.                         char[][] copy = copyGraph(grid);
  57.                         int ans = getFlipNum(copy, start / cols, start % cols);
  58.                         if(ans < res) {
  59.                                 res = ans;
  60.                         }
  61.                 }
  62.                 return res;
  63.         }

  64.         private char[][] copyGraph (char[][] grid) {
  65.                 char[][] copy = new char[grid.length][];
  66.                 for(int i = 0 ; i < grid.length ; i++) {
  67.                         copy[i] = Arrays.copyOfRange(grid[i], 0, grid[i].length);
  68.                 }
  69.                 return copy;
  70.         }
  71.         private int getFlipNum(char[][] grid, int row, int col) {
  72.                 int counter = 0;
  73.                 int rows = grid.length;
  74.                 int cols = grid[0].length;
  75.                 boolean flag = ifGetOneColor(grid);  // whether we get the grid with one color
  76.                 while (!flag) {
  77.                         counter++;
  78.                         char prevColor = grid[row][col];
  79.                         grid[row][col] = getNextCol(grid[row][col]);
  80.                         boolean[][] visited = new boolean[rows][cols];
  81.                         flipNeighbor(grid, row, col, prevColor, grid[row][col], visited);
  82.                         flag = ifGetOneColor(grid);
  83.                 }
  84.                 return counter;
  85.         }
  86.         private void flipNeighbor(char[][] grid, int i, int j,
  87.                         char prevColor, char currColor, boolean[][] visited) {
  88.                 for(int k = 0 ; k < dirs.length ; k++) {
  89.                         int x = i + dirs[k][0];
  90.                         int y = j + dirs[k][1];
  91.                         if(isValidate(grid, x, y) && !visited[x][y] &&
  92.                                         grid[x][y] == prevColor) {
  93.                                 visited[x][y] = true;
  94.                                 grid[x][y] = currColor;
  95.                                 flipNeighbor(grid, x, y, prevColor, currColor, visited);
  96.                         }
  97.                 }
  98.         }
  99.         private boolean ifGetOneColor(char[][] grid) {
  100.                 char color = grid[0][0];
  101.                 for(int i = 0 ; i < grid.length ; i++) {
  102.                         for(int j = 0 ; j < grid[0].length ; j++) {
  103.                                 if(grid[i][j] != color) {
  104.                                         return false;
  105.                                 }
  106.                         }
  107.                 }
  108.                 return true;
  109.         }
  110.         private char getNextCol(char curr) {
  111.                 int i = 0;
  112.                 for( ; i < order.length ; i++) {
  113.                         if(order[i] == curr) break;
  114.                 }
  115.                 if(i + 1 < order.length) return order[i + 1];
  116.                 else return order[0];
  117.         }
  118.         private boolean isValidate(char[][] grid, int i, int j) {
  119.                 return i < grid.length && i >= 0 && j >=0 && j < grid[0].length;
  120.         }
  121.         private class Union{
  122.                 int[] ids;
  123.                 int[] sizes;
  124.                 public Union(int len) {
  125.                         ids = new int[len];
  126.                         sizes = new int[len];
  127.                         for(int i = 0 ; i < ids.length ; i++) {
  128.                                 ids[i] = i;
  129.                                 sizes[i] = 1;
  130.                         }
  131.                 }
  132.                 public int find(int id) {
  133.                         if(ids[id] == id) return id;
  134.                         else {
  135.                                 int root = find(ids[id]);
  136.                                 // path compress
  137.                                 ids[id] = root;
  138.                                 return root;
  139.                         }
  140.                 }
  141.                 public void union(int id1, int id2) {
  142.                         int r1 = find(id1);
  143.                         int r2 = find(id2);
  144.                         if(r1 == r2) return ;
  145.                         else {
  146.                                 ids[r2] = r1;
  147.                                 sizes[r1] += sizes[r2];
  148.                         }
  149.                 }
  150.         }
  151. }
复制代码
回复

使用道具 举报

🔗
生姜979 2018-11-26 03:43:37 | 只看该作者
全局:
似乎只能bfs(dfs当然也行 只是慢一点) 
先union一下色块  对每个色块反转一次放到queue里  然后看是否都是一个颜色了
之后重复这几部的操作...
想想就觉得麻烦
回复

使用道具 举报

🔗
Ronald4545 2018-11-26 07:30:50 | 只看该作者
全局:
what does 依次翻转 mean?
回复

使用道具 举报

🔗
wegnahz 2018-11-26 14:58:16 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
wegnahz 2018-11-26 15:00:19 | 只看该作者
全局:
可能会有更好的贪心解,但是我感觉直接把色块最多的点的颜色当作终止颜色可能并不一定最优,枚举一下比较保险
回复

使用道具 举报

🔗
wluuuu 2019-1-3 12:16:21 | 只看该作者
全局:
可以请问楼主是在哪个office面的吗。。。
回复

使用道具 举报

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

本版积分规则

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