-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
62 lines (58 loc) · 1.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace DesignAFileSharingSystem
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
public class FileSharing
{
private int id;
private List<int> avaliableIds;
private Dictionary<int, HashSet<int>> system;
public FileSharing(int m)
{
id = 1;
avaliableIds = new List<int>();
system = new Dictionary<int, HashSet<int>>();
}
public int Join(IList<int> ownedChunks)
{
var userId = 0;
if (avaliableIds.Count != 0)
{
userId = avaliableIds[^1];
avaliableIds.RemoveAt(avaliableIds.Count - 1);
}
else
userId = id++;
system[userId] = new HashSet<int>(ownedChunks);
return userId;
}
public void Leave(int userID)
{
system.Remove(userID);
avaliableIds.Add(userID);
avaliableIds.Sort((a, b) => b - a);
}
public IList<int> Request(int userID, int chunkID)
{
var res = new List<int>();
foreach (var item in system)
{
if(item.Value.Contains(chunkID))
res.Add(item.Key);
}
if (res.Count == 0)
return res;
system[userID].Add(chunkID);
res.Sort();
return res;
}
}
}