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

Facebook Intern 面经

🔗
youngxie 2016-11-16 10:22:08 | 只看该作者
全局:
生成一个(w*h)大小的数组A, 初始化A[i] = i
for i=0 to m-1, swap(A[i], A[i + rand()%(w*h-i)]);


补充内容 (2016-11-16 10:27):
想想直接抽样就好,
for i=0 to m-1, A[i] = i+rand()%(w*h-i);

补充内容 (2016-11-16 10:36):
貌似有问题
回复

使用道具 举报

🔗
ccc666666 2016-11-16 14:18:40 | 只看该作者
全局:
同关注这个问题。。。
回复

使用道具 举报

🔗
eko910817 2016-11-16 14:19:50 | 只看该作者
全局:
关注followup。。。。。。
回复

使用道具 举报

🔗
Raymomd 2016-11-23 11:51:02 | 只看该作者
全局:
个人觉得要做到每个点被选为零的概率是m/(h*2)的话,复杂度不能做到O(m),可能面试官就是钓鱼执法。。。故意这么问的

补充内容 (2016-11-23 12:01):
打错字,m/(h*w)
回复

使用道具 举报

🔗
jinyyy666 2016-11-28 12:24:35 | 只看该作者
全局:
yun5260561 发表于 2016-11-14 05:31
感觉o(m)是能做的,就跟一维的一样

我也认为不可能做到O(m)啊。。。 上面的那个算法出来的概率应该不对的。 因为你到第二个雷,它的可以替换的range就少了右下角那个点,应该不太对的吧。。。

还有一个问题,就是如何保证产生的你第一个for loop里面每次产生的随机数不一样呢? 这个是无法做到的。如果有重复的话(即你的cur变量一样),在第二个loop里面不就出现两个雷被放到了同一个位子?这样应该不太对吧。。。
回复

使用道具 举报

🔗
changju0310 2016-11-28 12:25:29 | 只看该作者
全局:
求问楼主是网投简历吗?还是找了内推?
回复

使用道具 举报

🔗
jinyyy666 2016-11-28 13:02:10 | 只看该作者
全局:
yun5260561 发表于 2016-11-14 05:31
感觉o(m)是能做的,就跟一维的一样

感谢分享!但是我写了一下,发现你的code生成出来的matrix的概率不对。我写了reservoir sampling的code,O(hw)的,出来的概率就是对的。 我把code贴出来你看看哈:
  1. /**************************************************************
  2. * Problem: Given a matrix h*w, and randomly generate m mines
  3. *************************************************************/

  4. #include <vector>
  5. #include <iostream>

  6. using namespace std;
  7. class Solution{
  8. public:
  9.   // idea: reservoir sampling
  10.   vector<vector<int> > generate(int h, int w, int m){
  11.     vector<vector<int> > matrix(h, vector<int>(w, 0));
  12.     // reservoir sampling:
  13.     for(int i = 0; i < h*w; ++i){
  14.       if(i < m)  matrix[i/w][i%w] = i;
  15.       else{
  16.         int j = rand()%(i+1);
  17.         if(j < m)  matrix[j/w][j%w] = i;
  18.       }
  19.     }

  20.     // put the mines
  21.     for(int i = 0; i < m; ++i){
  22.       int val = matrix[i/w][i%w];
  23.       matrix[i/w][i%w] = 0;
  24.       matrix[val/w][val%w] = 1;
  25.     }
  26.     return matrix;
  27.   }
  28.   vector<vector<int> > generateOther(int h, int w, int m){
  29.     vector<vector<int> > matrix(h, vector<int>(w, 0));
  30.    
  31.     // other's approach:
  32.     int tail = h*w;
  33.     for(int i = 0; i < m; ++i){
  34.       tail--;
  35.       matrix[tail/w][tail%w] = rand()%(tail+1);
  36.     }

  37.     for(int i = 0; i < m; ++i){
  38.       int cur = matrix[tail/w][tail%w];
  39.       matrix[cur/w][cur%w] = 1;
  40.       matrix[tail/w][tail%w] = 0;
  41.       tail++;
  42.     }
  43.   
  44.     return matrix;
  45.   }
  46. };

  47. void printMatrix(vector<vector<int> > v){
  48.   int m = v.size(), n = v[0].size();
  49.   cout<<"***************************"<<endl;
  50.   for(int i = 0; i < m; ++i){
  51.     for(int j = 0; j < n; ++j)
  52.       cout<<v[i][j]<<" ";
  53.     cout<<endl;
  54.   }
  55.   cout<<"***************************"<<endl;
  56.   cout<<endl;
  57. }

  58. int main(int argc, char ** argv){
  59.   Solution s;
  60.   vector<vector<int> > target({{1,0,1},{0,0,0},{0,1,0}});
  61.   double cnt_r = 0, cnt_o;
  62.   for(int i = 0; i < 1000000; i++){
  63.     if(s.generateOther(3,3,3) == target)  cnt_o++;
  64.     if(s.generate(3,3,3) == target) cnt_r++;
  65.   }
  66.   cout<<"The probablity obtained under reservoir: "<<cnt_r/1000000.0<<endl;
  67.   cout<<"The probablity of another approach: "<<cnt_o/1000000.0<<endl;
  68.   cout<<"The ideal probablity is "<<6.0/(9*8*7)<<endl;
  69.   return 0;
  70. }

  71. /****************************************************
  72. * Output:
  73. * The probablity obtained under reservoir: 0.012127
  74. * The probablity of another approach: 0.003875
  75. * The idea probablity is 0.0119048
  76. ****************************************************/
复制代码
回复

使用道具 举报

🔗
ausgoo 2016-12-6 15:11:34 | 只看该作者
全局:
不知道这样写对不对。

  1. import random
  2. def solution(h, w, m):
  3.    
  4.     res = []
  5.     cnt = 0
  6.     for i in range(h):
  7.         for j in range(w):
  8.             if cnt < m:
  9.                 res.append((i, j))
  10.             else:
  11.                 if random.randint(1, cnt) <= m:
  12.                     res[random.randint(0, m-1)] = (i, j)
  13.             cnt += 1
  14.     return res
复制代码

补充内容 (2016-12-6 15:12):
只返回放雷的横纵坐标
回复

使用道具 举报

🔗
jolesiawu 2016-12-13 03:21:48 | 只看该作者
全局:
这题为啥不能用传统的reservoir sampling来做, 难道不是在h*w 里面选random subset of K? 为啥不能用这个方法啊?http://www.geeksforgeeks.org/reservoir-sampling/
赞同楼上写的,除了这两行
  1. if random.randint(1, cnt) <= m:
  2.                     res[random.randint(0, m-1)] = (i, j)
复制代码

这里我感觉应该用同一个数字而不是random两遍
回复

使用道具 举报

全局:
jolesiawu 发表于 2016-12-13 03:21
这题为啥不能用传统的reservoir sampling来做, 难道不是在h*w 里面选random subset of K? 为啥不能用这个 ...

赞同,的确需要保留之前的随机数
回复

使用道具 举报

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

本版积分规则

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