FB面经Prepare: Friends Recommendation
有个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的更多相关文章
- FB面经 Prepare: All Palindromic Substrings
Given a string, calculate how many substring is palindrome. Ignore non-char characters. Ignore case; ...
- FB面经 Prepare: Task Schedule
tasks has cooldown time, give an input task id array, output finish time input: AABCA A--ABCA output ...
- FB面经 Prepare: Make Parentheses valid
给一组括号,remove最少的括号使得它valid 从左从右各scan一次 package fb; public class removeParen { public static String fi ...
- FB面经Prepare: Dot Product
Conduct Dot Product of two large Vectors 1. two pointers 2. hashmap 3. 如果没有额外空间,如果一个很大,一个很小,适合scan小的 ...
- FB面经prepare: Count the number of Vector
给一个超级大的排好序的vector [abbcccdddeeee]比如,要求返回[{,a}, {,b}, {,c}, {,d}, {,e}......]复杂度要优于O(N) 分析: 如果是binary ...
- FB面经 Prepare: Largest Island
Find largest island in a board package fb; public class LargestIsland { public int findLargestIsland ...
- G面经prepare: Friends Recommendation
想想如果你用linkedin或者facebook, 给你一个人和他的朋友关系网,你会怎么给一个人推荐朋友 一个例子就是A-B, A-C, B - D, B - E, C - D,这个时候问我应该推荐谁 ...
- FB面经prepare: task schedule II
followup是tasks是无序的. 一开始是有序的,比如说1, 1, 2, 1,一定要先执行第一个task1,然后等task1恢复,再执行第2个task1,再执行task2..... follow ...
- FB面经prepare: Task Schedule
每种task都有冷却时间,比如task1执行后,要经过interval时间后才能再次执行,求总共所需时间. 用HashMap保存每一个task的下一次可以开始执行的最早时间 package TaskS ...
随机推荐
- for in和for of的区别(转)
原文链接:https://www.jianshu.com/p/c43f418d6bf0 1 遍历数组通常用for循环 ES5的话也可以使用forEach,ES5具有遍历数组功能的还有map.filte ...
- contos最小包安装完后一些准备
yum upgradeyum install net-toolsyum -y install wgetyum -y install vim-enhanced yum install gcc gcc-c ...
- Android-XML
Android-XML XML文件: <?xml version="1.0" encoding="utf-8"?> <books> &l ...
- XVII Open Cup named after E.V. Pankratiev. GP of Moscow Workshops
A. Centroid Tree 枚举至多两个重心作为根,检查对于每个点是否都满足$2size[x]\leq size[father[x]]$即可. #include<stdio.h> # ...
- [LeetCode] N-ary Tree Level Order Traversal N叉树层序遍历
Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 团队项目-课程MS需求分析心得
我们的课程管理项目需求讲道理其实应该是比较简单的,但是在经过几次和老师讨论过后,项目需求已经多得让人脑门疼,后来继续跟老师聊,老师嘴上说着减减减,但是每次讨论下来需求还是会变得更多,以致于个人已经不再 ...
- Codeforces 1154D - Walking Robot - [贪心]
题目链接:https://codeforces.com/contest/1154/problem/D 题解: 贪心思路,没有太阳的时候,优先用可充电电池走,万不得已才用普通电池走.有太阳的时候,如果可 ...
- zookeeper 日志输出到指定文件夹
最近在研究Zookeeper Storm Kafka, 顺便在本地搭了一套集群, 遇到了Zookeeper日志问题输出路径的问题, 发现zookeeper设置log4j.properties不能解决日 ...
- 通过使用Web Workers,Web应用程序可以在独立于主线程的后台线程中,运行一个脚本操作。这样做的好处是可以在独立线程中执行费时的处理任务,从而允许主线程(通常是UI线程)不会因此被阻塞/放慢。
Web Workers API - Web API 接口参考 | MDNhttps://developer.mozilla.org/zh-CN/docs/Web/API/Web_Workers_API ...
- spark-sql缩减版样例:获取每日top3搜索词和各自的次数,包括总次数
//获取出每天前3的搜索词 ArrayList<String> log = new ArrayList<String>(); log.add("2015-10-01, ...