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 ...
随机推荐
- Centos7/RHEL7 开启kdump
Kdump是一种基于kexec的Linux内核崩溃捕获机制,简单来说系统启动时会预留一块内存,当系统崩溃调用命令kexec(kdump kernel)在预留的内存中启动kdump内核, 该内核会将此时 ...
- net core体系-API-Restful+Swagger搭建API
本篇主要简单介绍下.net core下搭建WebApi 项目结构 项目结构其实不用多说,基本上大同小异. Controller:对外暴露的契约 Business/IBussiness:业务逻辑层实现及 ...
- Linux下Apache配置HTTPS功能
Apache配置HTTPS功能 转 https://www.cnblogs.com/liaojiafa/p/6028816.html 一.yum 安装openssl和openssl-devel,ht ...
- ubuntu下vim使用方法
按下's'可对文本进行编辑 按下'ESC'再输入':',之后输入wq是保存再退出,输入q是直接退出.如果是只读read only模式则需要输入'wq!'保存退出.
- centos7.4中安装docker
#!/bin/sh # 安装docker # 在docker中安装mysql # 解决了docker容器中无法输入中文的问题 ##########################安装docker # ...
- 安装linux
ctrl+alt tab键切换
- vue全选与取消全选
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python练手例子(8)
43.模仿静态变量(static)另一案例. 程序分析:演示一个python作用域使用方法. #python3.7 class Num: nNum = 1 def inc(self): self.nN ...
- applicationContext.xml和applicationContext-mvc.xml
1.applicationContext.xml<?xml version="1.0" encoding="UTF-8"?> <beans x ...
- jquery弹窗插件layer:layer.layui.com
这两天在做抽奖转盘功能,浏览器自带的alert弹出框太low,本人又基本不会前端, 于是借鉴前人用fancybox插件做的效果 结果没看懂其写法(http://www.0101shop.com/goo ...