Occupy and Clean Memory by Allocation ID
Problem statement
You are given a binary array memory, where 0 is free and 1 is initially occupied. Process each row in queries:
[0, k]: reserve the firstkcells of the leftmost free run having length at leastk. A successful allocation receives and returns the next positive allocation ID; failure returns -1 and does not consume an ID.[1, id]: free exactly the cells owned by the active allocationidand return its length. Return -1 for an unknown or already-cleaned ID.
Initially occupied cells belong to no allocation and cannot be cleaned.
Function
processMemoryAllocations(memory: int[], queries: int[][]) → int[]Examples
Example 1
memory = [0,0,1,0,0]queries = [[0,2],[0,1],[1,1],[0,2]]return = [1,2,2,3]The successful allocations receive IDs 1, 2, and 3; cleaning ID 1 frees two cells.
Example 2
memory = [1,0,0]queries = [[1,1],[0,3],[0,2],[1,1],[1,1],[0,3]]return = [-1,-1,1,2,-1,-1]Only the two-cell allocation succeeds. Its first cleanup frees two cells; repeated cleanup fails.
Constraints
1 <= memory.length,queries.length <= 2000memory[i]is 0 or 1.- Every query has exactly two integers and follows one of the stated forms.