Skip to content

Commit 3c450cb

Browse files
committed
feat: add solutions to lc problem: No.1500. Design a File Sharing System
1 parent 42843d8 commit 3c450cb

File tree

4 files changed

+288
-4
lines changed

4 files changed

+288
-4
lines changed

Diff for: solution/1500-1599/1500.Design a File Sharing System/README.md

+96-2
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,109 @@ fileSharing.join([]); // 一个不拥有任何文件块的用户加入系
9191
<!-- 这里可写当前语言的特殊实现逻辑 -->
9292

9393
```python
94-
94+
import queue
95+
96+
class FileSharing:
97+
98+
def __init__(self, m: int):
99+
self.cur = 0
100+
self.chunks = m
101+
self.reused = []
102+
self.user_chunks = collections.defaultdict(set)
103+
104+
def join(self, ownedChunks: List[int]) -> int:
105+
if self.reused:
106+
userID = heapq.heappop(self.reused)
107+
else:
108+
self.cur += 1
109+
userID = self.cur
110+
self.user_chunks[userID] = set(ownedChunks)
111+
return userID
112+
113+
def leave(self, userID: int) -> None:
114+
heapq.heappush(self.reused, userID)
115+
self.user_chunks.pop(userID)
116+
117+
def request(self, userID: int, chunkID: int) -> List[int]:
118+
if chunkID < 1 or chunkID > self.chunks:
119+
return []
120+
res = []
121+
for k, v in self.user_chunks.items():
122+
if chunkID in v:
123+
res.append(k)
124+
if res:
125+
self.user_chunks[userID].add(chunkID)
126+
return sorted(res)
127+
128+
# Your FileSharing object will be instantiated and called as such:
129+
# obj = FileSharing(m)
130+
# param_1 = obj.join(ownedChunks)
131+
# obj.leave(userID)
132+
# param_3 = obj.request(userID,chunkID)
95133
```
96134

97135
### **Java**
98136

99137
<!-- 这里可写当前语言的特殊实现逻辑 -->
100138

101139
```java
102-
140+
class FileSharing {
141+
private int chunks;
142+
private int cur;
143+
private TreeSet<Integer> reused;
144+
private TreeMap<Integer, Set<Integer>> userChunks;
145+
146+
public FileSharing(int m) {
147+
cur = 0;
148+
chunks = m;
149+
reused = new TreeSet<>();
150+
userChunks = new TreeMap<>();
151+
}
152+
153+
public int join(List<Integer> ownedChunks) {
154+
int userID;
155+
if (reused.isEmpty()) {
156+
++cur;
157+
userID = cur;
158+
} else {
159+
userID = reused.pollFirst();
160+
}
161+
userChunks.put(userID, new HashSet<>(ownedChunks));
162+
return userID;
163+
}
164+
165+
public void leave(int userID) {
166+
reused.add(userID);
167+
userChunks.remove(userID);
168+
}
169+
170+
public List<Integer> request(int userID, int chunkID) {
171+
if (chunkID < 1 || chunkID > chunks) {
172+
return Collections.emptyList();
173+
}
174+
List<Integer> res = new ArrayList<>();
175+
for (Map.Entry<Integer, Set<Integer>> entry : userChunks.entrySet()) {
176+
if (entry.getValue().contains(chunkID)) {
177+
res.add(entry.getKey());
178+
}
179+
}
180+
if (!userChunks.containsKey(userID)) {
181+
userChunks.put(userID, new HashSet<>());
182+
}
183+
if (!res.isEmpty()) {
184+
userChunks.get(userID).add(chunkID);
185+
}
186+
return res;
187+
}
188+
}
189+
190+
/**
191+
* Your FileSharing object will be instantiated and called as such:
192+
* FileSharing obj = new FileSharing(m);
193+
* int param_1 = obj.join(ownedChunks);
194+
* obj.leave(userID);
195+
* List<Integer> param_3 = obj.request(userID,chunkID);
196+
*/
103197
```
104198

105199
### **...**

Diff for: solution/1500-1599/1500.Design a File Sharing System/README_EN.md

+96-2
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,107 @@ fileSharing.join([]); // A user who doesn&#39;t have any chunks joined th
134134
### **Python3**
135135

136136
```python
137-
137+
import queue
138+
139+
class FileSharing:
140+
141+
def __init__(self, m: int):
142+
self.cur = 0
143+
self.chunks = m
144+
self.reused = []
145+
self.user_chunks = collections.defaultdict(set)
146+
147+
def join(self, ownedChunks: List[int]) -> int:
148+
if self.reused:
149+
userID = heapq.heappop(self.reused)
150+
else:
151+
self.cur += 1
152+
userID = self.cur
153+
self.user_chunks[userID] = set(ownedChunks)
154+
return userID
155+
156+
def leave(self, userID: int) -> None:
157+
heapq.heappush(self.reused, userID)
158+
self.user_chunks.pop(userID)
159+
160+
def request(self, userID: int, chunkID: int) -> List[int]:
161+
if chunkID < 1 or chunkID > self.chunks:
162+
return []
163+
res = []
164+
for k, v in self.user_chunks.items():
165+
if chunkID in v:
166+
res.append(k)
167+
if res:
168+
self.user_chunks[userID].add(chunkID)
169+
return sorted(res)
170+
171+
# Your FileSharing object will be instantiated and called as such:
172+
# obj = FileSharing(m)
173+
# param_1 = obj.join(ownedChunks)
174+
# obj.leave(userID)
175+
# param_3 = obj.request(userID,chunkID)
138176
```
139177

140178
### **Java**
141179

142180
```java
143-
181+
class FileSharing {
182+
private int chunks;
183+
private int cur;
184+
private TreeSet<Integer> reused;
185+
private TreeMap<Integer, Set<Integer>> userChunks;
186+
187+
public FileSharing(int m) {
188+
cur = 0;
189+
chunks = m;
190+
reused = new TreeSet<>();
191+
userChunks = new TreeMap<>();
192+
}
193+
194+
public int join(List<Integer> ownedChunks) {
195+
int userID;
196+
if (reused.isEmpty()) {
197+
++cur;
198+
userID = cur;
199+
} else {
200+
userID = reused.pollFirst();
201+
}
202+
userChunks.put(userID, new HashSet<>(ownedChunks));
203+
return userID;
204+
}
205+
206+
public void leave(int userID) {
207+
reused.add(userID);
208+
userChunks.remove(userID);
209+
}
210+
211+
public List<Integer> request(int userID, int chunkID) {
212+
if (chunkID < 1 || chunkID > chunks) {
213+
return Collections.emptyList();
214+
}
215+
List<Integer> res = new ArrayList<>();
216+
for (Map.Entry<Integer, Set<Integer>> entry : userChunks.entrySet()) {
217+
if (entry.getValue().contains(chunkID)) {
218+
res.add(entry.getKey());
219+
}
220+
}
221+
if (!userChunks.containsKey(userID)) {
222+
userChunks.put(userID, new HashSet<>());
223+
}
224+
if (!res.isEmpty()) {
225+
userChunks.get(userID).add(chunkID);
226+
}
227+
return res;
228+
}
229+
}
230+
231+
/**
232+
* Your FileSharing object will be instantiated and called as such:
233+
* FileSharing obj = new FileSharing(m);
234+
* int param_1 = obj.join(ownedChunks);
235+
* obj.leave(userID);
236+
* List<Integer> param_3 = obj.request(userID,chunkID);
237+
*/
144238
```
145239

146240
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class FileSharing {
2+
private int chunks;
3+
private int cur;
4+
private TreeSet<Integer> reused;
5+
private TreeMap<Integer, Set<Integer>> userChunks;
6+
7+
public FileSharing(int m) {
8+
cur = 0;
9+
chunks = m;
10+
reused = new TreeSet<>();
11+
userChunks = new TreeMap<>();
12+
}
13+
14+
public int join(List<Integer> ownedChunks) {
15+
int userID;
16+
if (reused.isEmpty()) {
17+
++cur;
18+
userID = cur;
19+
} else {
20+
userID = reused.pollFirst();
21+
}
22+
userChunks.put(userID, new HashSet<>(ownedChunks));
23+
return userID;
24+
}
25+
26+
public void leave(int userID) {
27+
reused.add(userID);
28+
userChunks.remove(userID);
29+
}
30+
31+
public List<Integer> request(int userID, int chunkID) {
32+
if (chunkID < 1 || chunkID > chunks) {
33+
return Collections.emptyList();
34+
}
35+
List<Integer> res = new ArrayList<>();
36+
for (Map.Entry<Integer, Set<Integer>> entry : userChunks.entrySet()) {
37+
if (entry.getValue().contains(chunkID)) {
38+
res.add(entry.getKey());
39+
}
40+
}
41+
if (!userChunks.containsKey(userID)) {
42+
userChunks.put(userID, new HashSet<>());
43+
}
44+
if (!res.isEmpty()) {
45+
userChunks.get(userID).add(chunkID);
46+
}
47+
return res;
48+
}
49+
}
50+
51+
/**
52+
* Your FileSharing object will be instantiated and called as such:
53+
* FileSharing obj = new FileSharing(m);
54+
* int param_1 = obj.join(ownedChunks);
55+
* obj.leave(userID);
56+
* List<Integer> param_3 = obj.request(userID,chunkID);
57+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import queue
2+
3+
class FileSharing:
4+
5+
def __init__(self, m: int):
6+
self.cur = 0
7+
self.chunks = m
8+
self.reused = []
9+
self.user_chunks = collections.defaultdict(set)
10+
11+
def join(self, ownedChunks: List[int]) -> int:
12+
if self.reused:
13+
userID = heapq.heappop(self.reused)
14+
else:
15+
self.cur += 1
16+
userID = self.cur
17+
self.user_chunks[userID] = set(ownedChunks)
18+
return userID
19+
20+
def leave(self, userID: int) -> None:
21+
heapq.heappush(self.reused, userID)
22+
self.user_chunks.pop(userID)
23+
24+
def request(self, userID: int, chunkID: int) -> List[int]:
25+
if chunkID < 1 or chunkID > self.chunks:
26+
return []
27+
res = []
28+
for k, v in self.user_chunks.items():
29+
if chunkID in v:
30+
res.append(k)
31+
if res:
32+
self.user_chunks[userID].add(chunkID)
33+
return sorted(res)
34+
35+
# Your FileSharing object will be instantiated and called as such:
36+
# obj = FileSharing(m)
37+
# param_1 = obj.join(ownedChunks)
38+
# obj.leave(userID)
39+
# param_3 = obj.request(userID,chunkID)

0 commit comments

Comments
 (0)