-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.java
54 lines (49 loc) · 1.47 KB
/
Solution.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
class FileSharing {
private int chunks;
private int cur;
private TreeSet<Integer> reused;
private TreeMap<Integer, Set<Integer>> userChunks;
public FileSharing(int m) {
cur = 0;
chunks = m;
reused = new TreeSet<>();
userChunks = new TreeMap<>();
}
public int join(List<Integer> ownedChunks) {
int userID;
if (reused.isEmpty()) {
++cur;
userID = cur;
} else {
userID = reused.pollFirst();
}
userChunks.put(userID, new HashSet<>(ownedChunks));
return userID;
}
public void leave(int userID) {
reused.add(userID);
userChunks.remove(userID);
}
public List<Integer> request(int userID, int chunkID) {
if (chunkID < 1 || chunkID > chunks) {
return Collections.emptyList();
}
List<Integer> res = new ArrayList<>();
for (Map.Entry<Integer, Set<Integer>> entry : userChunks.entrySet()) {
if (entry.getValue().contains(chunkID)) {
res.add(entry.getKey());
}
}
if (!res.isEmpty()) {
userChunks.computeIfAbsent(userID, k -> new HashSet<>()).add(chunkID);
}
return res;
}
}
/**
* Your FileSharing object will be instantiated and called as such:
* FileSharing obj = new FileSharing(m);
* int param_1 = obj.join(ownedChunks);
* obj.leave(userID);
* List<Integer> param_3 = obj.request(userID,chunkID);
*/