有个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. matplotlib 3D数据-【老鱼学matplotlib】

    直接上代码: import numpy as np import matplotlib.pyplot as plt # 导入显示3D的库 from mpl_toolkits.mplot3d impor ...

  2. open_basedir php授权目录设置

    php为了安全性考虑,有一项 open_basedir 的设置.根据你web服务器环境,open_basedir可以在几个地方设置. 首先 在php.ini中配置. ;open_basedir = 如 ...

  3. Python实现字符串反转的几种方法

    面试遇到的一个特无聊的问题--- 要求:在Python环境下用尽可能多的方法反转字符串,例如将s = "abcdef"反转成 "fedcba" 第一种:使用字符 ...

  4. ISP PIPLINE(零) 知识综述预热之光学概念篇

    1.光学成像关系如下:这是我看到最清晰的易懂的数学关系图 2.上面的知识了解完,camera应用的知识就是Autofocus技术,自动对焦 马达的起始位置一般在焦距处,由上面光学数学关系可知,焦距处可 ...

  5. XIII Open Grodno SU Championship

    A. Alice in the Wonderland 按题意模拟. #include<stdio.h> #include<iostream> #include<strin ...

  6. XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Peterhof

    A. City Wall 找规律. #include<stdio.h> #include<iostream> #include<string.h> #include ...

  7. Hadoop Java API 操作 hdfs--1

    Hadoop文件系统是一个抽象的概念,hdfs仅仅是Hadoop文件系统的其中之一. 就hdfs而言,访问该文件系统有两种方式:(1)利用hdfs自带的命令行方式,此方法类似linux下面的shell ...

  8. [LeetCode] Binary Tree Pruning 二叉树修剪

    We are given the head node root of a binary tree, where additionally every node's value is either a ...

  9. POJ 1324 Holedox Moving (状压BFS)

    POJ 1324 Holedox Moving (状压BFS) Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 18091 Acc ...

  10. vue发送请求----vue-resource

    使用插件vue-resource 官方提供的接口,在vue官网找不到 但在github中可以找到 安装:cnpm install vue-resource --save 第一步:注意要加--save, ...