-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1500-design-a-file-sharing-system.js
More file actions
89 lines (78 loc) · 2.65 KB
/
1500-design-a-file-sharing-system.js
File metadata and controls
89 lines (78 loc) · 2.65 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* 1500. Design a File Sharing System
* https://leetcode.com/problems/design-a-file-sharing-system/
* Difficulty: Medium
*
* We will use a file-sharing system to share a very large file which consists of m small
* chunks with IDs from 1 to m.
*
* When users join the system, the system should assign a unique ID to them. The unique ID
* should be used once for each user, but when a user leaves the system, the ID can be reused again.
*
* Users can request a certain chunk of the file, the system should return a list of IDs of
* all the users who own this chunk. If the user receives a non-empty list of IDs, they receive
* the requested chunk successfully.
*
* Implement the FileSharing class:
* - FileSharing(int m) Initializes the object with a file of m chunks.
* - int join(int[] ownedChunks): A new user joined the system owning some chunks of the file,
* the system should assign an id to the user which is the smallest positive integer not
* taken by any other user. Return the assigned id.
* - void leave(int userID): The user with userID will leave the system, you cannot take file
* chunks from them anymore.
* - int[] request(int userID, int chunkID): The user userID requested the file chunk with chunkID.
* Return a list of the IDs of all users that own this chunk sorted in ascending order.
*/
/**
* @param {number} m
*/
var FileSharing = function(m) {
this.chunkToUsers = new Array(m + 1).fill().map(() => new Set());
this.userToChunks = new Map();
this.availableIds = [];
this.nextId = 1;
};
/**
* @param {number[]} ownedChunks
* @return {number}
*/
FileSharing.prototype.join = function(ownedChunks) {
let userId;
if (this.availableIds.length > 0) {
userId = this.availableIds.sort((a, b) => a - b).shift();
} else {
userId = this.nextId++;
}
this.userToChunks.set(userId, new Set(ownedChunks));
for (const chunk of ownedChunks) {
this.chunkToUsers[chunk].add(userId);
}
return userId;
};
/**
* @param {number} userID
* @return {void}
*/
FileSharing.prototype.leave = function(userID) {
const userChunks = this.userToChunks.get(userID);
if (userChunks) {
for (const chunk of userChunks) {
this.chunkToUsers[chunk].delete(userID);
}
this.userToChunks.delete(userID);
this.availableIds.push(userID);
}
};
/**
* @param {number} userID
* @param {number} chunkID
* @return {number[]}
*/
FileSharing.prototype.request = function(userID, chunkID) {
const usersWithChunk = Array.from(this.chunkToUsers[chunkID]).sort((a, b) => a - b);
if (usersWithChunk.length > 0) {
this.userToChunks.get(userID).add(chunkID);
this.chunkToUsers[chunkID].add(userID);
}
return usersWithChunk;
};