传说中的谷歌招聘委员会成员之一,从幕后走出来,教你学系统设计!


一亩三分地论坛

 找回密码
 获取更多干活,快来注册
天天打游戏、照样领工资、还办H1B
这份工作你要不要?
把贵司招聘信息放这里
查看: 612|回复: 1
收起左侧

google footer level 4,送foobar challenge链接

[复制链接] |试试Instant~ |关注本帖
9oooop 发表于 2017-7-3 10:09:49 | 显示全部楼层 |阅读模式

2017(4-6月) 码农类 硕士 全职@Google - Other - 在线笔试 |Pass在职跳槽

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

您需要 登录 才可以下载或查看,没有帐号?获取更多干活,快来注册

x
关于google foobar这个博主介绍得很好:http://xiaohanyu.me/posts/2017-04-30-google-foobar-interview/ 我有好几题和他重复。贴一个遇到的level4题和我的解法。感觉gg foobar就是加点mathmatics的经典算法,high performance的implementation有难度,没有acm难。之后会贴个level5的新题。另外拿到个foobar challenge链接,想要的加米站内信,先到先得。如果要面试的话内推显然更快,主要是好玩。stem-font">Free the Bunny Prisoners
========================
. 1point 3acres 璁哄潧
You need to free the bunny prisoners before Commander Lambda's space station explodes! Unfortunately, the commander was very careful with her highest-value prisoners - they're all held in separate, maximum-security cells. The cells are opened by putting keys into each console, then pressing the open button on each console simultaneously. When the open button is pressed, each key opens its corresponding lock on the cell. So, the union of the keys in all of the consoles must be all of the keys. The scheme may require multiple copies of one key given to different minions.
. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
The consoles are far enough apart that a separate minion is needed for each one. Fortunately, you have already freed some bunnies to aid you - and even better, you were able to steal the keys while you were working as Commander Lambda's assistant. The problem is, you don't know which keys to use at which consoles. The consoles are programmed to know which keys each minion had, to prevent someone from just stealing all of the keys and using them blindly. There are signs by the consoles saying how many minions had some keys for the set of consoles. You suspect that Commander Lambda has a systematic way to decide which keys to give to each minion such that they could use the consoles.

You need to figure out the scheme that Commander Lambda used to distribute the keys. You know how many minions had keys, and how many consoles are by each cell. You know that Command Lambda wouldn't issue more keys than necessary (beyond what the key distribution scheme requires), and that you need as many bunnies with keys as there are consoles to open the cell.

Given the number of bunnies available and the number of locks required to open a cell, write a function answer(num_buns, num_required) which returns a specification of how to distribute the keys such that any num_required bunnies can open the locks, but no group of (num_required - 1) bunnies can.

Each lock is numbered starting from 0. The keys are numbered the same as the lock they open (so for a duplicate key, the number will repeat, since it opens the same lock). For a given bunny, the keys they get is represented as a sorted list of the numbers for the keys. To cover all of the bunnies, the final answer is represented by a sorted list of each individual bunny's list of keys. Find the lexicographically least such key distribution - that is, the first bunny should have keys sequentially starting from 0.

num_buns will always be between 1 and 9, and num_required will always be between 0 and 9 (both inclusive). For example, if you had 3 bunnies and required only 1 of them to open the cell, you would give each bunny the same key such that any of the 3 of them would be able to open it, like so:
[
[0],
[0],
[0],
]
If you had 2 bunnies and required both of them to open the cell, they would receive different keys (otherwise they wouldn't both actually be required), and your answer would be as follows:
[
. 1point3acres.com/bbs[0],
[1],
].1point3acres缃
Finally, if you had 3 bunnies and required 2 of them to open the cell, then any 2 of the 3 bunnies should have all of the keys necessary to open the cell, but no single bunny would be able to do it. Thus, the answer would be:
[
[0, 1],
[0, 2],
[1, 2],
]
. visit 1point3acres.com for more.

Languages
=========
.鐣欏璁哄潧-涓浜-涓夊垎鍦
To provide a Python solution, edit solution.py
To provide a Java solution, edit solution.java

Test cases. 1point 3acres 璁哄潧
==========

Inputs:-google 1point3acres
    (int) num_buns = 2
    (int) num_required = 1
Output:.鐣欏璁哄潧-涓浜-涓夊垎鍦
    (int) [[0], [0]]

Inputs:
    (int) num_buns = 5.鏈枃鍘熷垱鑷1point3acres璁哄潧
    (int) num_required = 3
Output: 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
    (int) [[0, 1, 2, 3, 4, 5], [0, 1, 2, 6, 7, 8], [0, 3, 4, 6, 7, 9], [1, 3, 5, 6, 8, 9], [2, 4, 5, 7, 8, 9]]

Inputs:
    (int) num_buns = 4. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
    (int) num_required = 4
Output:
    (int) [[0], [1], [2], [3]].1point3acres缃



 楼主| 9oooop 发表于 2017-7-3 10:18:38 | 显示全部楼层
想法是这个: https://en.wikipedia.org/wiki/Combination
  1. public class Answer {

  2.   public static int[][] answer(int num_buns, int num_required) {

  3.     if(num_required > num_buns || num_required == 0) return new int[num_buns][0];. more info on 1point3acres.com

  4.     if(num_required == 1) return new int[num_buns][1];
  5. . 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  6.     .鏈枃鍘熷垱鑷1point3acres璁哄潧

  7.     int[] bun_labels = new int[num_buns];

  8.     int combSize = num_buns - num_required + 1;

  9.     for(int i = 0; i < num_buns; i++) bun_labels[i] = i;

  10.     List<List<Integer>> schema = getCombinations(bun_labels, combSize, 0);. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴

  11.     -google 1point3acres

  12.     List<List<Integer>> result = new ArrayList<List<Integer>>();

  13.     for(int j = 0; j < num_buns; j++) result.add(new ArrayList<Integer>());-google 1point3acres

  14.    

  15.     for(int k = 0; k < schema.size(); k++) {

  16. . 1point 3acres 璁哄潧
  17.         List<Integer> positions = schema.get(k);

  18.         for(int pos : positions) { . From 1point 3acres bbs

  19.           result.get(pos).add(k);

  20.         }

  21.     }. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴

  22.    
  23. . more info on 1point3acres.com
  24.     int[][] ans = new int[num_buns][result.get(0).size()];

  25.     for(int c = 0; c < result.size(); c++) {

  26.         List<Integer> cur = result.get(c);
  27. . 1point 3acres 璁哄潧
  28.         for(int m = 0; m < cur.size(); m++) ans[c][m] = cur.get(m);

  29.     }

  30.     return ans;
  31. 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  32. }



  33. private static List<List<Integer>> getCombinations(int[] items, int combSize, int startIndx) {

  34.     List<List<Integer>> result = new ArrayList<List<Integer>>();

  35.     if (combSize == 0) return result;

  36.     if (combSize == 1) {

  37.         for(int i = startIndx; i < items.length; i++) {

  38.             List<Integer> cur = new ArrayList<Integer>();

  39.             cur.add(items[i]);

  40.             result.add(cur);

  41.         }

  42.     } else {

  43.         for(int i = startIndx; i <= items.length - combSize; i++){

  44.             List<List<Integer>> cur = getCombinations(items, combSize-1, i+1);

  45.             for (List<Integer> ele : cur) {. 1point 3acres 璁哄潧

  46.                 ele.add(0, i);
  47. . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  48.             }.鏈枃鍘熷垱鑷1point3acres璁哄潧

  49.             result.addAll(cur);

  50.         }
    . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴

  51.     }

  52.     return result;

  53. }

  54. }
复制代码
回复 支持 反对

使用道具 举报

本版积分规则

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明

custom counter

GMT+8, 2017-9-22 19:39

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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