@@ -85,7 +85,7 @@ loc.freeMemory(7); // 释放 mID 为 7 的所有内存单元。内存数组保
8585
8686<!-- solution:start -->
8787
88- ### 方法一:暴力模拟
88+ ### 方法一:模拟
8989
9090题目数据范围不大,可以直接用数组模拟内存空间。
9191
@@ -103,6 +103,7 @@ loc.freeMemory(7); // 释放 mID 为 7 的所有内存单元。内存数组保
103103
104104``` python
105105class Allocator :
106+
106107 def __init__ (self , n : int ):
107108 self .m = [0 ] * n
108109
@@ -118,7 +119,7 @@ class Allocator:
118119 return i - size + 1
119120 return - 1
120121
121- def free (self , mID : int ) -> int :
122+ def freeMemory (self , mID : int ) -> int :
122123 ans = 0
123124 for i, v in enumerate (self .m):
124125 if v == mID:
@@ -130,7 +131,7 @@ class Allocator:
130131# Your Allocator object will be instantiated and called as such:
131132# obj = Allocator(n)
132133# param_1 = obj.allocate(size,mID)
133- # param_2 = obj.free (mID)
134+ # param_2 = obj.freeMemory (mID)
134135```
135136
136137#### Java
@@ -156,7 +157,7 @@ class Allocator {
156157 return - 1 ;
157158 }
158159
159- public int free (int mID ) {
160+ public int freeMemory (int mID ) {
160161 int ans = 0 ;
161162 for (int i = 0 ; i < m. length; ++ i) {
162163 if (m[i] == mID) {
@@ -172,7 +173,7 @@ class Allocator {
172173 * Your Allocator object will be instantiated and called as such:
173174 * Allocator obj = new Allocator(n);
174175 * int param_1 = obj.allocate(size,mID);
175- * int param_2 = obj.free (mID);
176+ * int param_2 = obj.freeMemory (mID);
176177 */
177178```
178179
@@ -181,24 +182,26 @@ class Allocator {
181182``` cpp
182183class Allocator {
183184public:
185+ vector<int > m;
186+
184187 Allocator(int n) {
185- m = vector<int >(n);
188+ m = vector<int>(n, 0 );
186189 }
187190
188191 int allocate (int size, int mID) {
189192 int cnt = 0;
190193 for (int i = 0; i < m.size(); ++i) {
191- if (m[i]) {
194+ if (m[ i] > 0 ) {
192195 cnt = 0;
193196 } else if (++cnt == size) {
194- fill(i - size + 1, i + 1, mID);
197+ fill(m.begin() + i - size + 1, m.begin() + i + 1, mID);
195198 return i - size + 1;
196199 }
197200 }
198201 return -1;
199202 }
200203
201- int free (int mID) {
204+ int freeMemory (int mID) {
202205 int ans = 0;
203206 for (int i = 0; i < m.size(); ++i) {
204207 if (m[i] == mID) {
@@ -208,22 +211,13 @@ public:
208211 }
209212 return ans;
210213 }
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- }
220214};
221215
222216/**
223217 * Your Allocator object will be instantiated and called as such:
224218 * Allocator* obj = new Allocator(n);
225219 * int param_1 = obj->allocate(size,mID);
226- * int param_2 = obj->free (mID);
220+ * int param_2 = obj->freeMemory (mID);
227221 * /
228222```
229223
@@ -235,42 +229,85 @@ type Allocator struct {
235229}
236230
237231func Constructor(n int) Allocator {
238- return Allocator{make([]int, n)}
232+ return Allocator{m: make([]int, n)}
239233}
240234
241235func (this *Allocator) Allocate(size int, mID int) int {
242236 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 {
245239 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
253243 }
244+ return i - size + 1
254245 }
255246 }
256247 return -1
257248}
258249
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 {
262254 this.m[i] = 0
263255 ans++
264256 }
265257 }
266- return
258+ return ans
267259}
268260
269261/**
270262 * Your Allocator object will be instantiated and called as such:
271263 * obj := Constructor(n);
272264 * 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)
274311 */
275312```
276313
@@ -296,6 +333,7 @@ func (this *Allocator) Free(mID int) (ans int) {
296333
297334``` python
298335class Allocator :
336+
299337 def __init__ (self , n : int ):
300338 self .sl = SortedList([(- 1 , - 1 ), (n, n)])
301339 self .d = defaultdict(list )
@@ -309,7 +347,7 @@ class Allocator:
309347 return s
310348 return - 1
311349
312- def free (self , mID : int ) -> int :
350+ def freeMemory (self , mID : int ) -> int :
313351 ans = 0
314352 for block in self .d[mID]:
315353 self .sl.remove(block)
@@ -321,7 +359,7 @@ class Allocator:
321359# Your Allocator object will be instantiated and called as such:
322360# obj = Allocator(n)
323361# param_1 = obj.allocate(size,mID)
324- # param_2 = obj.free (mID)
362+ # param_2 = obj.freeMemory (mID)
325363```
326364
327365#### Java
@@ -353,9 +391,9 @@ class Allocator {
353391 return - 1 ;
354392 }
355393
356- public int free (int mID ) {
394+ public int freeMemory (int mID ) {
357395 int ans = 0 ;
358- for (int s : d. getOrDefault(mID, Collections . emptyList ())) {
396+ for (int s : d. getOrDefault(mID, List . of ())) {
359397 int e = tm. remove(s);
360398 ans += e - s + 1 ;
361399 }
@@ -368,7 +406,7 @@ class Allocator {
368406 * Your Allocator object will be instantiated and called as such:
369407 * Allocator obj = new Allocator(n);
370408 * int param_1 = obj.allocate(size,mID);
371- * int param_2 = obj.free (mID);
409+ * int param_2 = obj.freeMemory (mID);
372410 */
373411```
374412
@@ -398,7 +436,7 @@ public:
398436 return -1 ;
399437 }
400438
401- int free (int mID) {
439+ int freeMemory (int mID) {
402440 int ans = 0;
403441 for (int& s : d[ mID] ) {
404442 int e = tm[ s] ;
@@ -418,7 +456,7 @@ private:
418456 * Your Allocator object will be instantiated and called as such:
419457 * Allocator* obj = new Allocator(n);
420458 * int param_1 = obj->allocate(size,mID);
421- * int param_2 = obj->free (mID);
459+ * int param_2 = obj->freeMemory (mID);
422460 * /
423461```
424462
@@ -455,7 +493,7 @@ func (this *Allocator) Allocate(size int, mID int) int {
455493 return -1
456494}
457495
458- func (this *Allocator) Free (mID int) int {
496+ func (this *Allocator) FreeMemory (mID int) int {
459497 ans := 0
460498 for _, s := range this.d[mID] {
461499 if e, ok := this.rbt.Get(s); ok {
@@ -471,7 +509,7 @@ func (this *Allocator) Free(mID int) int {
471509 * Your Allocator object will be instantiated and called as such:
472510 * obj := Constructor(n);
473511 * param_1 := obj.Allocate(size,mID);
474- * param_2 := obj.Free (mID);
512+ * param_2 := obj.FreeMemory (mID);
475513 */
476514```
477515
0 commit comments