@@ -85,7 +85,7 @@ loc.freeMemory(7); // 释放 mID 为 7 的所有内存单元。内存数组保
85
85
86
86
<!-- solution:start -->
87
87
88
- ### 方法一:暴力模拟
88
+ ### 方法一:模拟
89
89
90
90
题目数据范围不大,可以直接用数组模拟内存空间。
91
91
@@ -103,6 +103,7 @@ loc.freeMemory(7); // 释放 mID 为 7 的所有内存单元。内存数组保
103
103
104
104
``` python
105
105
class Allocator :
106
+
106
107
def __init__ (self , n : int ):
107
108
self .m = [0 ] * n
108
109
@@ -118,7 +119,7 @@ class Allocator:
118
119
return i - size + 1
119
120
return - 1
120
121
121
- def free (self , mID : int ) -> int :
122
+ def freeMemory (self , mID : int ) -> int :
122
123
ans = 0
123
124
for i, v in enumerate (self .m):
124
125
if v == mID:
@@ -130,7 +131,7 @@ class Allocator:
130
131
# Your Allocator object will be instantiated and called as such:
131
132
# obj = Allocator(n)
132
133
# param_1 = obj.allocate(size,mID)
133
- # param_2 = obj.free (mID)
134
+ # param_2 = obj.freeMemory (mID)
134
135
```
135
136
136
137
#### Java
@@ -156,7 +157,7 @@ class Allocator {
156
157
return - 1 ;
157
158
}
158
159
159
- public int free (int mID ) {
160
+ public int freeMemory (int mID ) {
160
161
int ans = 0 ;
161
162
for (int i = 0 ; i < m. length; ++ i) {
162
163
if (m[i] == mID) {
@@ -172,7 +173,7 @@ class Allocator {
172
173
* Your Allocator object will be instantiated and called as such:
173
174
* Allocator obj = new Allocator(n);
174
175
* int param_1 = obj.allocate(size,mID);
175
- * int param_2 = obj.free (mID);
176
+ * int param_2 = obj.freeMemory (mID);
176
177
*/
177
178
```
178
179
@@ -181,24 +182,26 @@ class Allocator {
181
182
``` cpp
182
183
class Allocator {
183
184
public:
185
+ vector<int > m;
186
+
184
187
Allocator(int n) {
185
- m = vector<int >(n);
188
+ m = vector<int>(n, 0 );
186
189
}
187
190
188
191
int allocate (int size, int mID) {
189
192
int cnt = 0;
190
193
for (int i = 0; i < m.size(); ++i) {
191
- if (m[i]) {
194
+ if (m[ i] > 0 ) {
192
195
cnt = 0;
193
196
} else if (++cnt == size) {
194
- fill(i - size + 1, i + 1, mID);
197
+ fill(m.begin() + i - size + 1, m.begin() + i + 1, mID);
195
198
return i - size + 1;
196
199
}
197
200
}
198
201
return -1;
199
202
}
200
203
201
- int free (int mID) {
204
+ int freeMemory (int mID) {
202
205
int ans = 0;
203
206
for (int i = 0; i < m.size(); ++i) {
204
207
if (m[i] == mID) {
@@ -208,22 +211,13 @@ public:
208
211
}
209
212
return ans;
210
213
}
211
-
212
- private:
213
- vector<int > m;
214
-
215
- void fill(int from, int to, int val) {
216
- for (int i = from; i < to; ++i) {
217
- m[i] = val;
218
- }
219
- }
220
214
};
221
215
222
216
/**
223
217
* Your Allocator object will be instantiated and called as such:
224
218
* Allocator* obj = new Allocator(n);
225
219
* int param_1 = obj->allocate(size,mID);
226
- * int param_2 = obj->free (mID);
220
+ * int param_2 = obj->freeMemory (mID);
227
221
* /
228
222
```
229
223
@@ -235,42 +229,85 @@ type Allocator struct {
235
229
}
236
230
237
231
func Constructor(n int) Allocator {
238
- return Allocator{make([]int, n)}
232
+ return Allocator{m: make([]int, n)}
239
233
}
240
234
241
235
func (this *Allocator) Allocate(size int, mID int) int {
242
236
cnt := 0
243
- for i, v := range this.m {
244
- if v > 0 {
237
+ for i := 0; i < len( this.m); i++ {
238
+ if this.m[i] > 0 {
245
239
cnt = 0
246
- } else {
247
- cnt++
248
- if cnt == size {
249
- for j := i - size + 1; j <= i; j++ {
250
- this.m[j] = mID
251
- }
252
- return i - size + 1
240
+ } else if cnt++; cnt == size {
241
+ for j := i - size + 1; j <= i; j++ {
242
+ this.m[j] = mID
253
243
}
244
+ return i - size + 1
254
245
}
255
246
}
256
247
return -1
257
248
}
258
249
259
- func (this *Allocator) Free(mID int) (ans int) {
260
- for i, v := range this.m {
261
- if v == mID {
250
+ func (this *Allocator) FreeMemory(mID int) int {
251
+ ans := 0
252
+ for i := 0; i < len(this.m); i++ {
253
+ if this.m[i] == mID {
262
254
this.m[i] = 0
263
255
ans++
264
256
}
265
257
}
266
- return
258
+ return ans
267
259
}
268
260
269
261
/**
270
262
* Your Allocator object will be instantiated and called as such:
271
263
* obj := Constructor(n);
272
264
* param_1 := obj.Allocate(size,mID);
273
- * param_2 := obj.Free(mID);
265
+ * param_2 := obj.FreeMemory(mID);
266
+ */
267
+ ```
268
+
269
+ #### TypeScript
270
+
271
+ ``` ts
272
+ class Allocator {
273
+ private m: number [];
274
+
275
+ constructor (n : number ) {
276
+ this .m = Array (n ).fill (0 );
277
+ }
278
+
279
+ allocate(size : number , mID : number ): number {
280
+ let cnt = 0 ;
281
+ for (let i = 0 ; i < this .m .length ; i ++ ) {
282
+ if (this .m [i ] > 0 ) {
283
+ cnt = 0 ;
284
+ } else if (++ cnt === size ) {
285
+ for (let j = i - size + 1 ; j <= i ; j ++ ) {
286
+ this .m [j ] = mID ;
287
+ }
288
+ return i - size + 1 ;
289
+ }
290
+ }
291
+ return - 1 ;
292
+ }
293
+
294
+ freeMemory(mID : number ): number {
295
+ let ans = 0 ;
296
+ for (let i = 0 ; i < this .m .length ; i ++ ) {
297
+ if (this .m [i ] === mID ) {
298
+ this .m [i ] = 0 ;
299
+ ans ++ ;
300
+ }
301
+ }
302
+ return ans ;
303
+ }
304
+ }
305
+
306
+ /**
307
+ * Your Allocator object will be instantiated and called as such:
308
+ * var obj = new Allocator(n)
309
+ * var param_1 = obj.allocate(size,mID)
310
+ * var param_2 = obj.freeMemory(mID)
274
311
*/
275
312
```
276
313
@@ -296,6 +333,7 @@ func (this *Allocator) Free(mID int) (ans int) {
296
333
297
334
``` python
298
335
class Allocator :
336
+
299
337
def __init__ (self , n : int ):
300
338
self .sl = SortedList([(- 1 , - 1 ), (n, n)])
301
339
self .d = defaultdict(list )
@@ -309,7 +347,7 @@ class Allocator:
309
347
return s
310
348
return - 1
311
349
312
- def free (self , mID : int ) -> int :
350
+ def freeMemory (self , mID : int ) -> int :
313
351
ans = 0
314
352
for block in self .d[mID]:
315
353
self .sl.remove(block)
@@ -321,7 +359,7 @@ class Allocator:
321
359
# Your Allocator object will be instantiated and called as such:
322
360
# obj = Allocator(n)
323
361
# param_1 = obj.allocate(size,mID)
324
- # param_2 = obj.free (mID)
362
+ # param_2 = obj.freeMemory (mID)
325
363
```
326
364
327
365
#### Java
@@ -353,9 +391,9 @@ class Allocator {
353
391
return - 1 ;
354
392
}
355
393
356
- public int free (int mID ) {
394
+ public int freeMemory (int mID ) {
357
395
int ans = 0 ;
358
- for (int s : d. getOrDefault(mID, Collections . emptyList ())) {
396
+ for (int s : d. getOrDefault(mID, List . of ())) {
359
397
int e = tm. remove(s);
360
398
ans += e - s + 1 ;
361
399
}
@@ -368,7 +406,7 @@ class Allocator {
368
406
* Your Allocator object will be instantiated and called as such:
369
407
* Allocator obj = new Allocator(n);
370
408
* int param_1 = obj.allocate(size,mID);
371
- * int param_2 = obj.free (mID);
409
+ * int param_2 = obj.freeMemory (mID);
372
410
*/
373
411
```
374
412
@@ -398,7 +436,7 @@ public:
398
436
return -1 ;
399
437
}
400
438
401
- int free (int mID) {
439
+ int freeMemory (int mID) {
402
440
int ans = 0;
403
441
for (int& s : d[ mID] ) {
404
442
int e = tm[ s] ;
@@ -418,7 +456,7 @@ private:
418
456
* Your Allocator object will be instantiated and called as such:
419
457
* Allocator* obj = new Allocator(n);
420
458
* int param_1 = obj->allocate(size,mID);
421
- * int param_2 = obj->free (mID);
459
+ * int param_2 = obj->freeMemory (mID);
422
460
* /
423
461
```
424
462
@@ -455,7 +493,7 @@ func (this *Allocator) Allocate(size int, mID int) int {
455
493
return -1
456
494
}
457
495
458
- func (this *Allocator) Free (mID int) int {
496
+ func (this *Allocator) FreeMemory (mID int) int {
459
497
ans := 0
460
498
for _, s := range this.d[mID] {
461
499
if e, ok := this.rbt.Get(s); ok {
@@ -471,7 +509,7 @@ func (this *Allocator) Free(mID int) int {
471
509
* Your Allocator object will be instantiated and called as such:
472
510
* obj := Constructor(n);
473
511
* param_1 := obj.Allocate(size,mID);
474
- * param_2 := obj.Free (mID);
512
+ * param_2 := obj.FreeMemory (mID);
475
513
*/
476
514
```
477
515
0 commit comments