-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2502.cpp
More file actions
47 lines (44 loc) · 1.13 KB
/
2502.cpp
File metadata and controls
47 lines (44 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Allocator {
public:
vector<int> memory;
int N;
Allocator(int n) {
memory.resize(n, 0);
this->N = n;
}
int allocate(int size, int mID) {
int index = 0;
while (index < N) {
if (memory[index] == 0) {
int start = index;
while (index + 1 < N && memory[index + 1] == 0 && index - start + 1 < size) {
index++;
}
if (index - start + 1 >= size) {
for (int i = 0; i < size; ++i) {
memory[start + i] = mID;
}
return start;
}
}
index++;
}
return -1;
}
int free(int mID) {
int cnt = 0;
for (int i = 0; i < N; ++i) {
if (memory[i] == mID) {
cnt++;
memory[i] = 0;
}
}
return cnt;
}
};
/**
* 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);
*/