Class Allocator
java.lang.Object
g2501_2600.s2502_design_memory_allocator.Allocator
2502 - Design Memory Allocator.<p>Medium</p>
<p>You are given an integer <code>n</code> representing the size of a <strong>0-indexed</strong> memory array. All memory units are initially free.</p>
<p>You have a memory allocator with the following functionalities:</p>
<ol>
<li><strong>Allocate</strong> a block of <code>size</code> consecutive free memory units and assign it the id <code>mID</code>.</li>
<li><strong>Free</strong> all memory units with the given id <code>mID</code>.</li>
</ol>
<p><strong>Note</strong> that:</p>
<ul>
<li>Multiple blocks can be allocated to the same <code>mID</code>.</li>
<li>You should free all the memory units with <code>mID</code>, even if they were allocated in different blocks.</li>
</ul>
<p>Implement the <code>Allocator</code> class:</p>
<ul>
<li><code>Allocator(int n)</code> Initializes an <code>Allocator</code> object with a memory array of size <code>n</code>.</li>
<li><code>int allocate(int size, int mID)</code> Find the <strong>leftmost</strong> block of <code>size</code> <strong>consecutive</strong> free memory units and allocate it with the id <code>mID</code>. Return the block’s first index. If such a block does not exist, return <code>-1</code>.</li>
<li><code>int free(int mID)</code> Free all memory units with the id <code>mID</code>. Return the number of memory units you have freed.</li>
</ul>
<p><strong>Example 1:</strong></p>
<p><strong>Input</strong> [“Allocator”, “allocate”, “allocate”, “allocate”, “free”, “allocate”, “allocate”, “allocate”, “free”, “allocate”, “free”] [[10], [1, 1], [1, 2], [1, 3], [2], [3, 4], [1, 1], [1, 1], [1], [10, 2], [7]]</p>
<p><strong>Output:</strong> [null, 0, 1, 2, 1, 3, 1, 6, 3, -1, 0]</p>
<p><strong>Explanation:</strong></p>
<p>Allocator loc = new Allocator(10); // Initialize a memory array of size 10. All memory units are initially free.</p>
<p>loc.allocate(1, 1); // The leftmost block’s first index is 0. The memory array becomes [<strong>1</strong> ,_,_,_,_,_,_,_,_,_]. We return 0.</p>
<p>loc.allocate(1, 2); // The leftmost block’s first index is 1. The memory array becomes [1, <strong>2</strong> ,_,_,_,_,_,_,_,_]. We return 1.</p>
<p>loc.allocate(1, 3); // The leftmost block’s first index is 2. The memory array becomes [1,2, <strong>3</strong> ,_,_,_,_,_,_,_]. We return 2.</p>
<p>loc.free(2); // Free all memory units with mID 2. The memory array becomes [1,_, 3,_,_,_,_,_,_,_]. We return 1 since there is only 1 unit with mID 2.</p>
<p>loc.allocate(3, 4); // The leftmost block’s first index is 3. The memory array becomes [1,_,3, <strong>4</strong> , <strong>4</strong> , <strong>4</strong> ,_,_,_,_]. We return 3.</p>
<p>loc.allocate(1, 1); // The leftmost block’s first index is 1. The memory array becomes [1, <strong>1</strong> ,3,4,4,4,_,_,_,_]. We return 1.</p>
<p>loc.allocate(1, 1); // The leftmost block’s first index is 6. The memory array becomes [1,1,3,4,4,4, <strong>1</strong> ,_,_,_]. We return 6.</p>
<p>loc.free(1); // Free all memory units with mID 1. The memory array becomes [_,_,3,4,4,4,_,_,_,_]. We return 3 since there are 3 units with mID 1.</p>
<p>loc.allocate(10, 2); // We can not find any free block with 10 consecutive free memory units, so we return -1.</p>
<p>loc.free(7); // Free all memory units with mID 7. The memory array remains the same since there is no memory unit with mID 7. We return 0.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n, size, mID <= 1000</code></li>
<li>At most <code>1000</code> calls will be made to <code>allocate</code> and <code>free</code>.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Allocator
public Allocator(int n)
-
-
Method Details
-
allocate
public int allocate(int size, int mID) -
free
public int free(int mID) -
collapse
public int collapse(g2501_2600.s2502_design_memory_allocator.Allocator.Node cur, int id)
-