-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetWatchedVideosbyYourFriends.java
71 lines (66 loc) · 2.12 KB
/
GetWatchedVideosbyYourFriends.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Entry{
int freq;
String vid;
public Entry(int freq, String vid){
this.freq = freq;
this.vid = vid;
}
}
class Solution {
public List<String> watchedVideosByFriends(List<List<String>> watchedVideos, int[][] friends, int id, int level) {
ArrayList<String> videos = new ArrayList<>();
for (int i:bfs(id, level,friends)){
videos.addAll(watchedVideos.get(i));
}
Map<String, Integer> map = new HashMap<>();
for (String i:videos){
if (map.containsKey(i)){
int val = map.get(i);
map.replace(i, ++val);
}else{
map.put(i, 1);
}
}
PriorityQueue<Entry> pq = new PriorityQueue<Entry>(
new Comparator<Entry>(){
@Override
public int compare(Entry e1, Entry e2){
if(e1.freq!=e2.freq){
return e1.freq-e2.freq;
}
return e1.vid.compareTo(e2.vid);
}
});
for (String key:map.keySet()){
pq.offer(new Entry(map.get(key), key));
}
ArrayList<String> ans = new ArrayList<>();
while (!pq.isEmpty()){
ans.add(pq.poll().vid);
}
return ans;
}
private List<Integer> bfs(int start, int level, int[][] friends){
Queue<Integer> queue = new LinkedList<>();
Set<Integer> visited = new HashSet<>();
queue.offer(start);
int currentLevel = 0;
while (!queue.isEmpty()){
if (currentLevel >= level){
break;
}
ArrayList<Integer> subqueue = new ArrayList<>(queue);
queue = new LinkedList<>();
for (int i:subqueue){
visited.add(i);
for (int j:friends[i]){
if (!visited.contains(j) && !queue.contains(j) && !subqueue.contains(j)){
queue.offer(j);
}
}
}
currentLevel++;
}
return new ArrayList<Integer>(queue);
}
}