想想如果你用linkedin或者facebook, 给你一个人和他的朋友关系网,你会怎么给一个人推荐朋友

一个例子就是A-B, A-C, B - D, B - E, C - D,这个时候问我应该推荐谁给A,我说D,因为他是BC的共同好友,而E只是B的好友,到这我才明白干啥,就是给一个图和里面的一个节点A,用bfs从A出发,找出第二层中indegree度数最大节点

用HashMap<Character, HashSet<Character>>来建图

用HashMap<Character, Integer> SndIndegree来表示第二层indegree数目

用maxIndegree记录第二层Indegree最大值

用res记录第二层Indegree最大者

BFS

 package FriendRecommendation;
import java.util.*; public class Solution {
public char recommend(char tar, char[][] arr) {
HashMap<Character, HashSet<Character>> graph = new HashMap<Character, HashSet<Character>>();
HashMap<Character, Integer> SndIndegree = new HashMap<Character, Integer>(); //build graph
for (char[] edge : arr) {
if (!graph.containsKey(edge[0])) graph.put(edge[0], new HashSet<Character>());
if (!graph.containsKey(edge[1])) graph.put(edge[1], new HashSet<Character>());
graph.get(edge[0]).add(edge[1]);
if (!SndIndegree.containsKey(edge[0])) SndIndegree.put(edge[0], 0);
if (!SndIndegree.containsKey(edge[1])) SndIndegree.put(edge[1], 0);
} Queue<Character> queue = new LinkedList<Character>();
HashSet<Character> visited = new HashSet<Character>();
int level = 0;
queue.offer(tar);
visited.add(tar);
int PNum = 1;
int CNum = 0;
int maxIndegree = 0;
char res = '\0';
while (!queue.isEmpty()) {
char cur = queue.poll();
PNum--;
for (Character neigh : graph.get(cur)) {
if (level+1 == 2) {
if (neigh == tar) continue;
int curIndegree = SndIndegree.get(neigh)+1;
if (curIndegree > maxIndegree) res = neigh.charValue();
SndIndegree.put(neigh, curIndegree);
}
else { //not second level
if (!visited.contains(neigh)) {
queue.offer(neigh);
CNum++;
visited.add(neigh);
}
}
}
if (PNum == 0) {
PNum = CNum;
CNum = 0;
level++;
}
}
return res;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution sol = new Solution();
char res = sol.recommend('A', new char[][]{{'A','B'},{'A','C'},{'B','D'},{'B','E'},{'C','D'},{'B','A'},{'C','A'}});
System.out.println(res);
} }

G面经prepare: Friends Recommendation的更多相关文章

  1. G面经prepare: Set Intersection && Set Difference

    求两个sorted数组的intersection e.g. [1,2,3,4,5],[2,4,6] 结果是[2,4] difference 类似merge, 分小于等于大于三种情况,然后时间O(m+n ...

  2. G面经prepare: Reorder String to make duplicates not consecutive

    字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...

  3. FB面经Prepare: Friends Recommendation

    有个getFriend() API, 让你推荐你的朋友的朋友做你的朋友,当然这个新朋友不能是你原来的老朋友 package fb; import java.util.*; public class R ...

  4. G面经prepare: Maximum Subsequence in Another String's Order

    求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...

  5. G面经prepare: Pattern Match

    设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...

  6. G面经prepare: Data Stream Average

    给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...

  7. G面经prepare: Android Phone Unlock Pattern

    1 2 3 4 5 6 7 8 9 只有中间没有其他键的两个键才能相连,比如1可以连 2 4 5 6 8 但不能连 3 7 9 但是如果中间键被使用了,那就可以连,比如5已经被使用了,那1就可以连9 ...

  8. G面经prepare: Jump Game Return to Original Place

    第二题 算法 给你一个arr 返回 T 或者 F arr的每个数代表从这个点开始跳几部,返回T的情况:从这个arr中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素 比如[ ...

  9. G面经prepare: Sort String Based On Another

    Given a sorting order string, sort the input string based on the given sorting order string. Ex sort ...

随机推荐

  1. 【转载】C编译过程概述

    gcc:http://www.cnblogs.com/ggjucheng/archive/2011/12/14/2287738.html#_Toc311642844 gdb:http://www.cn ...

  2. corresponding SQLSTATE values general error

    http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html B.3 Server Error Codes and Message ...

  3. java中重载和覆盖(又称重写)的区别

    初次见到这两个单词并没有什么特别的感觉,但是时间长了,却发现书上一会儿用override,一会儿又用overload,搞得我的迷迷糊.于是就做了个总结,希望能对和我一样对这两个概念模糊不清的网友有一个 ...

  4. Ngrok 内网穿透利器

    Ngrok是什么 Ngrok 是一个反向代理,通过在公共的端点和本地运行的 Web 服务器之间建立一个安全的通道.Ngrok 可捕获和分析所有通道上的流量,便于后期分析和重放 为什么要使用Ngrok ...

  5. python的变量

    Python变量 在Python中,变量的概念基本上和初中代数的方程变量是一致的.例如,对于方程式 y=x*x ,x就是变量.当x=2时,计算结果是4,当x=5时,计算结果是25. 只是在计算机程序中 ...

  6. mysql select 练习题

    10. 查询Score表中的最高分的学生学号和课程号.(子查询或者排序) select sno,cno from score where degree  in(select max(degree) f ...

  7. centos apache安装和设置

    分类: LINUX 安装方式:yum install httpdyum install mysql-serveryum install phpyum install php-mysql 一.WEB服务 ...

  8. HBase的安装部署以及简单使用

    一:下载安装 1.下载安装 2.开启hadoop与zookeeper 3.修改配置文件hbase-env export JAVA_HOME=/opt/modules/jdk1.7.0_67 expor ...

  9. html5优势

    1.首先,强化了Web网页的表现性能.除了可描绘二维图形外,还准备了用于播放视频和音频的标签.2.其次,追加了本地数据库等Web应用的功能.3.HTML5(text/html)浏览器将在错误语法的处理 ...

  10. 【android学习4】Eclipse中Clean作用

    今天修改Servlet中代码,重启服务端程序之后发现没有启作用,于是Clean了一把,果然生效. 查阅资料得知,Eclipse中是根据时间戳去编译代码,如果某个类对应的时间戳没有发生改变就不会重新编译 ...