-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
67 lines (66 loc) · 2.17 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace GetWatchedVideosByYourFriends
{
class Program
{
static void Main(string[] args)
{
var watchedVideos = new List<IList<string>>
{
new List<string> {"A", "B"},
new List<string> {"C"},
new List<string> {"B", "C"},
new List<string> {"D"},
};
var friends = new int[][]
{
new[] {1, 2},
new[] {0, 3},
new[] {0, 3},
new[] {1, 2},
};
int id = 0, level = 2;
Console.WriteLine(WatchedVideosByFriends(watchedVideos, friends, id, level));
}
static IList<string> WatchedVideosByFriends(IList<IList<string>> watchedVideos, int[][] friends, int id, int level)
{
var queue = new Queue<int>();
queue.Enqueue(id);
var visited = new HashSet<int> {id};
var levelFriends = new HashSet<int>();
while (queue.Count != 0)
{
var count = queue.Count;
level--;
for (int i = 0; i < count; i++)
{
var cur = queue.Dequeue();
foreach (var next in friends[cur])
{
if (visited.Add(next))
{
queue.Enqueue(next);
if (level == 0)
levelFriends.Add(next);
}
}
}
if(level == 0) break;
}
var dict = new Dictionary<string, int>();
foreach (var friend in levelFriends)
{
var videos = watchedVideos[friend];
foreach (var video in videos)
{
if (!dict.ContainsKey(video))
dict[video] = 0;
dict[video]++;
}
}
return dict.OrderBy(x => x.Value).ThenBy(x => x.Key).Select(x => x.Key).ToList();
}
}
}