Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.2502 #4107

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 81 additions & 43 deletions solution/2500-2599/2502.Design Memory Allocator/README.md
Original file line number Diff line number Diff line change
@@ -85,7 +85,7 @@ loc.freeMemory(7); // 释放 mID 为 7 的所有内存单元。内存数组保

<!-- solution:start -->

### 方法一:暴力模拟
### 方法一:模拟

题目数据范围不大,可以直接用数组模拟内存空间。

@@ -103,6 +103,7 @@ loc.freeMemory(7); // 释放 mID 为 7 的所有内存单元。内存数组保

```python
class Allocator:

def __init__(self, n: int):
self.m = [0] * n

@@ -118,7 +119,7 @@ class Allocator:
return i - size + 1
return -1

def free(self, mID: int) -> int:
def freeMemory(self, mID: int) -> int:
ans = 0
for i, v in enumerate(self.m):
if v == mID:
@@ -130,7 +131,7 @@ class Allocator:
# Your Allocator object will be instantiated and called as such:
# obj = Allocator(n)
# param_1 = obj.allocate(size,mID)
# param_2 = obj.free(mID)
# param_2 = obj.freeMemory(mID)
```

#### Java
@@ -156,7 +157,7 @@ class Allocator {
return -1;
}

public int free(int mID) {
public int freeMemory(int mID) {
int ans = 0;
for (int i = 0; i < m.length; ++i) {
if (m[i] == mID) {
@@ -172,7 +173,7 @@ class Allocator {
* Your Allocator object will be instantiated and called as such:
* Allocator obj = new Allocator(n);
* int param_1 = obj.allocate(size,mID);
* int param_2 = obj.free(mID);
* int param_2 = obj.freeMemory(mID);
*/
```

@@ -181,24 +182,26 @@ class Allocator {
```cpp
class Allocator {
public:
vector<int> m;

Allocator(int n) {
m = vector<int>(n);
m = vector<int>(n, 0);
}

int allocate(int size, int mID) {
int cnt = 0;
for (int i = 0; i < m.size(); ++i) {
if (m[i]) {
if (m[i] > 0) {
cnt = 0;
} else if (++cnt == size) {
fill(i - size + 1, i + 1, mID);
fill(m.begin() + i - size + 1, m.begin() + i + 1, mID);
return i - size + 1;
}
}
return -1;
}

int free(int mID) {
int freeMemory(int mID) {
int ans = 0;
for (int i = 0; i < m.size(); ++i) {
if (m[i] == mID) {
@@ -208,22 +211,13 @@ public:
}
return ans;
}

private:
vector<int> m;

void fill(int from, int to, int val) {
for (int i = from; i < to; ++i) {
m[i] = val;
}
}
};

/**
* Your Allocator object will be instantiated and called as such:
* Allocator* obj = new Allocator(n);
* int param_1 = obj->allocate(size,mID);
* int param_2 = obj->free(mID);
* int param_2 = obj->freeMemory(mID);
*/
```

@@ -235,42 +229,85 @@ type Allocator struct {
}

func Constructor(n int) Allocator {
return Allocator{make([]int, n)}
return Allocator{m: make([]int, n)}
}

func (this *Allocator) Allocate(size int, mID int) int {
cnt := 0
for i, v := range this.m {
if v > 0 {
for i := 0; i < len(this.m); i++ {
if this.m[i] > 0 {
cnt = 0
} else {
cnt++
if cnt == size {
for j := i - size + 1; j <= i; j++ {
this.m[j] = mID
}
return i - size + 1
} else if cnt++; cnt == size {
for j := i - size + 1; j <= i; j++ {
this.m[j] = mID
}
return i - size + 1
}
}
return -1
}

func (this *Allocator) Free(mID int) (ans int) {
for i, v := range this.m {
if v == mID {
func (this *Allocator) FreeMemory(mID int) int {
ans := 0
for i := 0; i < len(this.m); i++ {
if this.m[i] == mID {
this.m[i] = 0
ans++
}
}
return
return ans
}

/**
* Your Allocator object will be instantiated and called as such:
* obj := Constructor(n);
* param_1 := obj.Allocate(size,mID);
* param_2 := obj.Free(mID);
* param_2 := obj.FreeMemory(mID);
*/
```

#### TypeScript

```ts
class Allocator {
private m: number[];

constructor(n: number) {
this.m = Array(n).fill(0);
}

allocate(size: number, mID: number): number {
let cnt = 0;
for (let i = 0; i < this.m.length; i++) {
if (this.m[i] > 0) {
cnt = 0;
} else if (++cnt === size) {
for (let j = i - size + 1; j <= i; j++) {
this.m[j] = mID;
}
return i - size + 1;
}
}
return -1;
}

freeMemory(mID: number): number {
let ans = 0;
for (let i = 0; i < this.m.length; i++) {
if (this.m[i] === mID) {
this.m[i] = 0;
ans++;
}
}
return ans;
}
}

/**
* Your Allocator object will be instantiated and called as such:
* var obj = new Allocator(n)
* var param_1 = obj.allocate(size,mID)
* var param_2 = obj.freeMemory(mID)
*/
```

@@ -296,6 +333,7 @@ func (this *Allocator) Free(mID int) (ans int) {

```python
class Allocator:

def __init__(self, n: int):
self.sl = SortedList([(-1, -1), (n, n)])
self.d = defaultdict(list)
@@ -309,7 +347,7 @@ class Allocator:
return s
return -1

def free(self, mID: int) -> int:
def freeMemory(self, mID: int) -> int:
ans = 0
for block in self.d[mID]:
self.sl.remove(block)
@@ -321,7 +359,7 @@ class Allocator:
# Your Allocator object will be instantiated and called as such:
# obj = Allocator(n)
# param_1 = obj.allocate(size,mID)
# param_2 = obj.free(mID)
# param_2 = obj.freeMemory(mID)
```

#### Java
@@ -353,9 +391,9 @@ class Allocator {
return -1;
}

public int free(int mID) {
public int freeMemory(int mID) {
int ans = 0;
for (int s : d.getOrDefault(mID, Collections.emptyList())) {
for (int s : d.getOrDefault(mID, List.of())) {
int e = tm.remove(s);
ans += e - s + 1;
}
@@ -368,7 +406,7 @@ class Allocator {
* Your Allocator object will be instantiated and called as such:
* Allocator obj = new Allocator(n);
* int param_1 = obj.allocate(size,mID);
* int param_2 = obj.free(mID);
* int param_2 = obj.freeMemory(mID);
*/
```

@@ -398,7 +436,7 @@ public:
return -1;
}

int free(int mID) {
int freeMemory(int mID) {
int ans = 0;
for (int& s : d[mID]) {
int e = tm[s];
@@ -418,7 +456,7 @@ private:
* Your Allocator object will be instantiated and called as such:
* Allocator* obj = new Allocator(n);
* int param_1 = obj->allocate(size,mID);
* int param_2 = obj->free(mID);
* int param_2 = obj->freeMemory(mID);
*/
```

@@ -455,7 +493,7 @@ func (this *Allocator) Allocate(size int, mID int) int {
return -1
}

func (this *Allocator) Free(mID int) int {
func (this *Allocator) FreeMemory(mID int) int {
ans := 0
for _, s := range this.d[mID] {
if e, ok := this.rbt.Get(s); ok {
@@ -471,7 +509,7 @@ func (this *Allocator) Free(mID int) int {
* Your Allocator object will be instantiated and called as such:
* obj := Constructor(n);
* param_1 := obj.Allocate(size,mID);
* param_2 := obj.Free(mID);
* param_2 := obj.FreeMemory(mID);
*/
```

Loading