有个getFriend() API, 让你推荐你的朋友的朋友做你的朋友,当然这个新朋友不能是你原来的老朋友
 package fb;

 import java.util.*;

 public class ReferFriends {
public List<String> recommendation(String name) {
List<String> res = new ArrayList<String>();
if (name==null || name.length()==0) return res; List<String> friends = getFriends(name);
HashSet<String> set = new HashSet<String>();
for (String friend : friends) {
set.add(friend);
} HashMap<String, Integer> map = new HashMap<String, Integer>();
ArrayList<String>[] list = new ArrayList[friends.size()+1]; for (String friend : friends) {
List<String> ffriends = getFriends(friend);
for (String each : ffriends) {
if (!set.contains(each) && !each.equals(name)) {
//map.put(each, map.getOrDefault(each, 0) + 1);
if (map.containsKey(each))
map.put(each, map.get(each)+1);
else map.put(each, 1);
}
}
} for (String each : map.keySet()) {
int count = map.get(each);
if (list[count] == null) list[count] = new ArrayList<String>();
list[count].add(each);
} for (int k=list.length-1; k>=0; k--) {
if (list[k] != null)
res.addAll(list[k]);
}
return res; } public List<String> getFriends(String name) {
HashMap<String, List<String>> map = new HashMap<String, List<String>>();
map.put("Lily", new ArrayList<String>());
map.put("Lucy", new ArrayList<String>());
map.put("Hanmeimei", new ArrayList<String>());
map.put("Jim", new ArrayList<String>());
map.put("Lilei", new ArrayList<String>());
map.put("Poly", new ArrayList<String>());
map.get("Lily").add("Lilei");
map.get("Lily").add("Poly");
map.get("Lily").add("Hanmeimei");
map.get("Lily").add("Jim");
map.get("Lucy").add("Lilei");
map.get("Lucy").add("Poly");
map.get("Lucy").add("Hanmeimei");
map.get("Lucy").add("Lily");
map.get("Lilei").add("Jim");
map.get("Lilei").add("Lucy");
map.get("Lilei").add("UncleWang");
map.get("Lilei").add("Hanmeimei");
map.get("Lilei").add("Lily");
map.get("Jim").add("Lily");
map.get("Jim").add("Lucy");
map.get("Jim").add("Lilei");
map.get("Poly").add("Jim");
map.get("Poly").add("Lilei");
return map.get(name);
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ReferFriends sol = new ReferFriends();
//List<String> res = sol.recommendation("Poly");
List<String> res = sol.recommendation("Jim");
for (String each : res) {
System.out.println(each);
}
} }

FB面经Prepare: Friends Recommendation的更多相关文章

  1. FB面经 Prepare: All Palindromic Substrings

    Given a string, calculate how many substring is palindrome. Ignore non-char characters. Ignore case; ...

  2. FB面经 Prepare: Task Schedule

    tasks has cooldown time, give an input task id array, output finish time input: AABCA A--ABCA output ...

  3. FB面经 Prepare: Make Parentheses valid

    给一组括号,remove最少的括号使得它valid 从左从右各scan一次 package fb; public class removeParen { public static String fi ...

  4. FB面经Prepare: Dot Product

    Conduct Dot Product of two large Vectors 1. two pointers 2. hashmap 3. 如果没有额外空间,如果一个很大,一个很小,适合scan小的 ...

  5. FB面经prepare: Count the number of Vector

    给一个超级大的排好序的vector [abbcccdddeeee]比如,要求返回[{,a}, {,b}, {,c}, {,d}, {,e}......]复杂度要优于O(N) 分析: 如果是binary ...

  6. FB面经 Prepare: Largest Island

    Find largest island in a board package fb; public class LargestIsland { public int findLargestIsland ...

  7. G面经prepare: Friends Recommendation

    想想如果你用linkedin或者facebook, 给你一个人和他的朋友关系网,你会怎么给一个人推荐朋友 一个例子就是A-B, A-C, B - D, B - E, C - D,这个时候问我应该推荐谁 ...

  8. FB面经prepare: task schedule II

    followup是tasks是无序的. 一开始是有序的,比如说1, 1, 2, 1,一定要先执行第一个task1,然后等task1恢复,再执行第2个task1,再执行task2..... follow ...

  9. FB面经prepare: Task Schedule

    每种task都有冷却时间,比如task1执行后,要经过interval时间后才能再次执行,求总共所需时间. 用HashMap保存每一个task的下一次可以开始执行的最早时间 package TaskS ...

随机推荐

  1. 关闭 Window 之后,无法设置 Visibility,也无法调用 Show、ShowDialogor 或 WindowInteropHelper.EnsureHandle。

    参考网址: 1.WPF Tips: Window.ShowDialog()方法:Cannot set Visibility or call Show, ShowDialog, or WindowInt ...

  2. spark算子

    1.map 一条一条读取 def map(): Unit ={ val list = List("张无忌", "赵敏", "周芷若") va ...

  3. GCD Counting-树形DP

    GCD Counting 思路: 预处理  每个权值的素因子.问题转化为  以同一个素数作为因子 最长的链, 树形DP求解,ans 由 此点的 最长子链 + 次长子链 相加得到, 然后再更新最长子链 ...

  4. WIN10 拨号连接下 如何开启移动热点

    错误提示为:我们无法设置移动热点,因为你的电脑未建立以太网,WIFI或手机网络连接. 解决方法: 1. 首先用手机或其他设备建立无线热点.  2. 电脑连接步骤1中的热点,电脑端打开移动热点.  3. ...

  5. 使用第三方插件Gear Tacks 画齿轮

    以下介绍第二种方法: 重复再生成一个大的齿轮 两个都保存起来: 再创建一个装配体环境. 接下来就是要达到使两个轮子配合转起来的效果! 步骤如下:

  6. 图片懒加载、selenium和PhantomJS

    1.图片懒加载 1.1 概念及实现原理 图片懒加载是一种网页优化技术.图片作为一种网络资源,在被请求时也与普通静态资源一样,将占用网络资源,而一次性将整个页面的所有图片加载完, 将大大增加页面的首屏加 ...

  7. python的array初识

    from array import array """ 'b' signed char int 1 'B' unsigned char int 1 'u' Py_UNIC ...

  8. JDBC API 可滚动可编辑的结果集

    JDBC的API中的链接数据和创建statement并且执行读取ResultSet大家已经很熟悉了,这边介绍设置statement的属性使结果集可以移动并且进行编辑同步回数据库. Statement ...

  9. Educational Codeforces Round 7

    622A - Infinite Sequence    20171123 暴力枚举\(n\)在哪个区间即可,时间复杂度为\(O(\sqrt{n})\) #include<stdlib.h> ...

  10. swust oj 237

    Calculate Sum-Of-Absolute-Differences 1000(ms) 65535(kb) 131 / 683 给你N个正整数,求两两之差的绝对值之和. 比如有4个数分别为 3, ...