G面经prepare: Friends Recommendation
想想如果你用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的更多相关文章
- 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 ...
- G面经prepare: Reorder String to make duplicates not consecutive
字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...
- FB面经Prepare: Friends Recommendation
有个getFriend() API, 让你推荐你的朋友的朋友做你的朋友,当然这个新朋友不能是你原来的老朋友 package fb; import java.util.*; public class R ...
- G面经prepare: Maximum Subsequence in Another String's Order
求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...
- G面经prepare: Pattern Match
设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...
- G面经prepare: Data Stream Average
给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...
- 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 ...
- G面经prepare: Jump Game Return to Original Place
第二题 算法 给你一个arr 返回 T 或者 F arr的每个数代表从这个点开始跳几部,返回T的情况:从这个arr中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素 比如[ ...
- 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 ...
随机推荐
- Asp.net 服务器Application,Session,Cookie,ViewState和Cache区别
2.8 Context 的使用Context 对象包含与当前页面相关的信息,提供对整个上下文的访问,包括请求.响应.以及上文中的Session 和Application 等信息.可以使用此对象在网页之 ...
- changing a pointer rather than erasing memory cells
Computer Science An Overview _J. Glenn Brookshear _11th Edition In a data structure based on a point ...
- http://d3js.org/
http://d3js.org/ http://www.ourd3js.com/wordpress/?p=51 http://www.ourd3js.com/wordpress/?p=104file: ...
- ajax普通弹窗;Bootstrp弹窗
1.普通弹窗 主页面: <head> <meta http-equiv="Content-Type" content="text/html; chars ...
- Python基本数据类型之int 、 float
首先要明确的是:在python中,一切皆为对象. 从底层角度看,对象就是保存在内存中的一个数据块.从抽象层看,对象就是我们的代码模拟出的一个类的独立个体. 在python中变量不需要声明类型,也不需要 ...
- Windows系统结合MinGW搭建软件开发环境
MinGW介绍 MinGW,即Minimalist GNU For Windows,它包含了GNU工具集的运行环境.GCC编译器工具集以及其它的GNU程序开发工具(如make.gawk.grep等等) ...
- hadoop-2.7.3 在windows环境下安装(无需Cygwin)
http://blog.csdn.net/kokjuis/article/details/53537029
- linux 开机启动设置
操作系统:Ubuntu12.04硬件环境:HP CQ45 当用户使用sudo apt-get install安装完apache和mysql之后,这些服务默认是开机启动的,但是有的时候需要 ...
- 一个例子深入理解ClassLoader
文件类加载器,该加载器重载了loadClass方法,逻辑是只读取文件来加载类,不委托给父类加载器进行加载 package com.ydd.study.hello.classloader; import ...
- Xcode编译WebApps找不到js的错误解决办法<转>
使用Xcode做WebApps时,使用UIWebview来调用一个页面,有时会遇到问题,其一就是编译的时候出现黄色感叹号的Warning,js文件都报错:warning: no rule to pro ...