Skip to content

Commit 4d5e782

Browse files
committed
https://leetcode.cn/problems/design-a-file-sharing-system/
1 parent a0394ed commit 4d5e782

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Step 2. Add the dependency
4545

4646
<summary>展开查看</summary>
4747

48+
https://leetcode.cn/problems/design-a-file-sharing-system/
49+
4850
https://leetcode.cn/problems/append-k-integers-with-minimal-sum/
4951

5052
https://leetcode.cn/problems/inorder-successor-in-bst-ii/

design-a-file-sharing-system/index.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
export default class FileSharing {
2+
#chunkUsersMap = new Map<number, Set<number>>();
3+
4+
#userChunksMap = new Map<number, Set<number>>();
5+
#userQueue = new BinaryHeap<number>((a, b) => a - b);
6+
#maxUser = 0;
7+
constructor(_m: number) {}
8+
9+
join(ownedChunks: number[]): number {
10+
let userID = 0;
11+
12+
if (this.#userQueue.isEmpty()) {
13+
this.#maxUser++;
14+
userID = this.#maxUser;
15+
} else {
16+
userID = this.#userQueue.pop() as number;
17+
}
18+
19+
for (const chunk of ownedChunks) {
20+
const users = this.#chunkUsersMap.get(chunk) ?? new Set();
21+
users.add(userID);
22+
this.#chunkUsersMap.set(chunk, users);
23+
}
24+
this.#userChunksMap.set(userID, new Set(ownedChunks));
25+
return userID;
26+
}
27+
leave(userID: number) {
28+
if (this.#userChunksMap.has(userID)) {
29+
const chunks = this.#userChunksMap.get(userID) ?? [];
30+
for (const chunk of chunks) {
31+
if (this.#chunkUsersMap.has(chunk)) {
32+
const users = this.#chunkUsersMap.get(chunk) as Set<number>;
33+
users.delete(userID);
34+
if (users.size > 0) this.#chunkUsersMap.set(chunk, users);
35+
else this.#chunkUsersMap.delete(chunk);
36+
}
37+
}
38+
this.#userChunksMap.delete(userID);
39+
}
40+
41+
this.#userQueue.push(userID);
42+
}
43+
44+
request(userID: number, chunkID: number): Array<number> {
45+
const usersList = Array<number>();
46+
if (
47+
!this.#chunkUsersMap.has(chunkID) || !this.#userChunksMap.has(userID)
48+
) return [];
49+
50+
usersList.push(...(this.#chunkUsersMap.get(chunkID) ?? []));
51+
52+
const users = this.#chunkUsersMap.get(chunkID) as Set<number>;
53+
users.add(userID);
54+
55+
this.#chunkUsersMap.set(chunkID, users);
56+
57+
const chunks = this.#userChunksMap.get(userID) as Set<number>;
58+
59+
chunks.add(chunkID);
60+
this.#userChunksMap.set(userID, chunks);
61+
usersList.sort((a, b) => a - b);
62+
return usersList;
63+
}
64+
}
65+
import { BinaryHeap } from "https://deno.land/[email protected]/collections/binary_heap.ts";

design-a-file-sharing-system/test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { assertEquals } from "asserts";
2+
import { runScript } from "leetcode-class";
3+
4+
import FileSharing from "./index.ts";
5+
6+
Deno.test("design-a-file-sharing-system", () => {
7+
assertEquals(
8+
runScript(
9+
[
10+
"FileSharing",
11+
"join",
12+
"join",
13+
"join",
14+
"request",
15+
"request",
16+
"leave",
17+
"request",
18+
"leave",
19+
"join",
20+
],
21+
[
22+
[4],
23+
[[1, 2]],
24+
[[2, 3]],
25+
[[4]],
26+
[1, 3],
27+
[2, 2],
28+
[1],
29+
[2, 1],
30+
[2],
31+
[[]],
32+
],
33+
FileSharing,
34+
),
35+
[null, 1, 2, 3, [2], [1, 2], null, [], null, 1],
36+
);
37+
});

0 commit comments

Comments
 (0)