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

FB onsite

全局:

2017(4-6月) 码农类General 博士 全职@meta - 内推 - Onsite  | | Fail | 在职跳槽

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

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

x
3/31 电面 top k nearest points.
4/24 onsite:
        1. read4k, one time
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
今天接到电话已挂,说是有一轮coding没有给出optimal solution


上一篇:BB 店面 5/8
下一篇:facebook面筋

本帖被以下淘专辑推荐:

推荐
axieyanga 2017-5-18 13:25:27 | 只看该作者
全局:
top k nearest points.  最优解是 quick selection 吧
  1.   public static class Point{
  2.         int x;
  3.         int y;
  4.         double distance;
  5.         public Point(int x , int y){
  6.             this.x = x;
  7.             this.y = y;
  8.             distance=0;
  9.         }
  10.         @Override
  11.         public String toString(){
  12.             return "["+this.x+","+this.y+"]";
  13.         }
  14.     }
  15.    
  16.     public List<Point> topKPoints(List<Point> points, Point target, int k){
  17.          List<Point>  ret = new ArrayList<Point>();
  18.         for(Point p : points){
  19.             p.distance = distance(p,target);
  20.         }
  21.         
  22.         partition(points,0,points.size()-1, k);
  23.         for(int i=0;i<k;i++){
  24.             ret.add(points.get(i));
  25.         }
  26.         return ret;
  27.     }
  28.    
  29.    
  30.     public void partition(List<Point> arr, int lo, int hi, int k){
  31.         int i=lo; int j=hi;
  32.         while(i<j){
  33.             int offset =quickSelect(arr, i, j);
  34.             if(offset ==k-1) return;
  35.             else if(offset<k-1){
  36.                 i=offset+1;
  37.             }
  38.             else {
  39.                 j=offset-1;
  40.             }
  41.         }
  42.     }
  43.     @SuppressWarnings("empty-statement")
  44.     public int quickSelect(List<Point> arr, int lo, int hi){
  45.         int i=lo; int j =hi+1;
  46.         while(true){
  47.             while(arr.get(++i).distance<arr.get(lo).distance)
  48.                 if(i==hi) break;
  49.             while(arr.get(--j).distance>arr.get(lo).distance);
  50.             if(i>=j) break;
  51.             exch(arr, i, j);
  52.         }
  53.         exch(arr, lo, j);
  54.         return j;
  55.     }
  56.    
  57.     public void exch(List<Point> arr, int i, int j){
  58.         Point temp = arr.get(i);
  59.         arr.set(i, arr.get(j));
  60.         arr.set(j,temp);
  61.     }
  62.    
  63.     public double distance(Point a, Point b){
  64.         return Math.sqrt((a.x -b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
  65.     }
复制代码

补充内容 (2017-5-18 13:26):
复杂度 O(N) , 如果是 用pq的话 复杂度 为 O(NlgK)
回复

使用道具 举报

推荐
 楼主| pc27149 2017-5-9 16:13:51 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

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

使用道具 举报

🔗
FightOn 2017-5-9 07:04:21 | 只看该作者
全局:
lz不会是被黑了吧?这两个题的最优解不是地里都有么?
回复

使用道具 举报

🔗
vickyer 2017-5-9 09:23:23 | 只看该作者
全局:
...还是感谢楼主分享!楼主可以说说POI怎么答有中面是面试官的方向吗?
回复

使用道具 举报

🔗
say543 2017-5-16 14:51:29 | 只看该作者
全局:
How to reduce copy.  read4 这题怎么达的 ?
回复

使用道具 举报

🔗
f1371342385 2017-5-16 15:10:56 | 只看该作者
全局:
LZ是那一轮没有给出最优解呀,除了第一轮的follow up,其余看起来都很正常呀
回复

使用道具 举报

🔗
hakusama1024 2017-5-24 07:00:52 | 只看该作者
全局:
axieyanga 发表于 2017-5-18 13:25
top k nearest points.  最优解是 quick selection 吧

请问quick select如果选前5大的数是要跑5次么?
回复

使用道具 举报

🔗
axieyanga 2017-5-26 02:14:12 | 只看该作者
全局:
hakusama1024 发表于 2017-5-24 07:00
请问quick select如果选前5大的数是要跑5次么?

不是啊 , 假如总共 n 个点, 不管找 top k , 复杂度永远是 logn 不是k
回复

使用道具 举报

🔗
hakusama1024 2017-5-26 05:16:26 | 只看该作者
全局:
axieyanga 发表于 2017-5-26 02:14
不是啊 , 假如总共 n 个点, 不管找 top k , 复杂度永远是 logn 不是k

logn就可以么。请问是怎么做的。
回复

使用道具 举报

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

本版积分规则

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