回复: 17
跳转到指定楼层
上一主题 下一主题
收起左侧

谷歌onsite新题

全局:

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

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

x
给定一个二维数组,有不同的颜色,一个连通的区域叫做一种色块,一开始可以选择一个色块,比如说选了r,那么r会对应一个反转序列 比如说是 rgb...,可以依次翻转,每次翻转之后相邻的相同
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
都看不到了,12月还有最后一次面试,鸭梨山大,还有一些最近面的题目会陆续发出来,求各位加点大米。

评分

参与人数 8大米 +29 收起 理由
NicholasJW + 3 给你点个赞!
caomei6 + 3 给你点个赞!
lzhong + 3 给你点个赞!
Rex_Coding + 5 给你点个赞!
SDAY + 5 很有用的信息!

查看全部评分


上一篇:狗狗实习oa
下一篇:airbnb的水流问题求解

本帖被以下淘专辑推荐:

推荐
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
回复

使用道具 举报

推荐
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. }
复制代码
回复

使用道具 举报

推荐
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
回复

使用道具 举报

🔗
msu_HIDDEN 2018-11-23 08:00:06 | 只看该作者
全局:
感觉是那个字符翻转的变种?
回复

使用道具 举报

🔗
 楼主| LuckySheep 2018-11-23 08:21:58 | 只看该作者
全局:
msu_HIDDEN 发表于 2018-11-23 08:00
感觉是那个字符翻转的变种?

你说的是926的话和那个解法没什么关系。
回复

使用道具 举报

🔗
msu_HIDDEN 2018-11-23 08:33:55 | 只看该作者
全局:
LuckySheep 发表于 2018-11-23 08:21
你说的是926的话和那个解法没什么关系。

https://www.1point3acres.com/bbs/thread-443299-1-1.html

这贴里的第一题
回复

使用道具 举报

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

使用道具 举报

全局:
这题我猜应该就是数有多少独立的岛屿数目,number of island变种
回复

使用道具 举报

全局:
不是啊 这不就是暴力解用bfs直接traverse嘛
回复

使用道具 举报

🔗
Rex_Coding 2018-11-24 06:22:45 | 只看该作者
全局:
先用union find合并所有的色块,因为翻转色块和翻转一个格子的花费是一样的,然后统计所有unions中出现颜色最多的,把这个颜色当作最后的颜色花费一定是最小的,然后依次翻转所有union就好。我感觉思路是这样。
回复

使用道具 举报

🔗
索克斯10115 2018-11-24 06:28:50 | 只看该作者
全局:
请问一下楼主, 翻转序列是想当前的颜色根据翻转序,转为其他颜色 是吗
回复

使用道具 举报

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

使用道具 举报

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

本版积分规则

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