forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
38 lines (34 loc) · 863 Bytes
/
Solution.java
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
class Allocator {
private int[] m;
public Allocator(int n) {
m = new int[n];
}
public int allocate(int size, int mID) {
int cnt = 0;
for (int i = 0; i < m.length; ++i) {
if (m[i] > 0) {
cnt = 0;
} else if (++cnt == size) {
Arrays.fill(m, i - size + 1, i + 1, mID);
return i - size + 1;
}
}
return -1;
}
public int free(int mID) {
int ans = 0;
for (int i = 0; i < m.length; ++i) {
if (m[i] == mID) {
m[i] = 0;
++ans;
}
}
return ans;
}
}
/**
* 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);
*/