有个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. 理解上下文Context

    --摘自<Android进阶解密> 知识点: 1.Context的使用场景 1)使用Context调用方法,比如启动Activity.访问资源.调用系统级服务等 2)调用方法时传入Cont ...

  2. Problem J. Journey with Pigs

    Problem J. Journey with Pigshttp://codeforces.com/gym/241680/problem/J考察排序不等式算出来单位重量在每个村庄的收益,然后生序排列猪 ...

  3. python 进程间通信(上)

    一  使用queue来实现进程间的内存共享 #_*_coding:utf-8_*_ from multiprocessing import Process,Queue import os,time d ...

  4. 在VS2010上安装MVC4(webApi)

    我们安装的VS2010上是没有MVC4或者WebApi的,要想加入这些功能只能自己在网上下载安装. 要安装MVC4,首先得安装VS10sp(Service Package)1,然后再安装MVC4.安装 ...

  5. IDEA激活方式(亲测有效)加汉化方式

    2018/12/3 最新破解方法 将0.0.0.0 account.jetbrains.com保存到本地host文件中 然后使用注册码 K71U8DBPNE-eyJsaWNlbnNlSWQiOiJLN ...

  6. 复杂链表的复制(Hard)

    问题来源:选自LeetCode 138:复制带随机指针的链表 问题描述: 题目给定信息: 该链表中每一个节点的成员变量都有两个,一个是next指针指向该节点的下一个节点,一个是random指针指向不确 ...

  7. selenium3 TestNG 介绍与配置

    一.TestNG介绍 我之前有学习过Junit,Nunit 这些工具,现在想看看TestNG,那么TestNG是什么呢?他们之间有什么区别呢? TestNG(Next Generation)是一个测试 ...

  8. vue_ajax 请求

    yarn add vue-resource axios npm install --save axios pubsub-js // import VueResource from "vue- ...

  9. SSM的,日常错误

    org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.imooc ...

  10. 最近发现一个php trim的bug

    用trim 排除字符串两边的 “.”:你会发现“耀.”会出现编码错误问题,导致程序出现错误!代码如下: $a = "王者荣耀."; echo trim($a,".&quo ...