Problem Brief
Memory Allocator
FULLTIMEOA
Given a memory array consisting of 0s and 1s (0 = free, 1 = occupied). Memory is aligned by 8.
Support the following two operations, provided as a list of strings:
alloc x— Find the leftmost contiguous block ofxfree units whose starting index is divisible by 8. Allocate it (mark as occupied and assign a unique ID). Return the starting index, or-1if not found.erase id— Scan the entire array, reset all positions with the given ID to 0, and return the number of cleared units.
IDs are assigned incrementally starting from 1. Given the initial memory array and a list of operations, return an array of integer results — one per operation.
Note: The starting index of an allocated block must be divisible by 8. The block itself may span beyond the next multiple of 8.
1Example 1
Input
memory = [0,0,0,0,0,0,0,0], operations = ["alloc 3","erase 1"]
Output
[0, 3]
Explanation
alloc 3: starting index 0 is divisible by 8, positions 0–2 are all free → allocate with ID=1, return 0. erase 1: positions 0–2 have ID=1 → clear them and return 3.
2Example 2
Input
memory = [1,1,1,1,1,1,1,1], operations = ["alloc 1"]
Output
[-1]
Explanation
alloc 1: the only valid starting index is 0, but position 0 is occupied → return -1.