Problem · Design
Reusable Number Allocator
Learn this problemProblem statement
Implement a reusable allocator for the integers from 0 through n - 1.
The allocator starts with every number available. Process the parallel arrays operations and values from left to right:
assignreturns and reserves the smallest currently available number. If no number is available, it returns-1. Its corresponding entry invaluesis-1.releasemakesvalues[i]available again. Every release is guaranteed to name a number that is currently assigned. A release produces no output.
Return the results of all assign operations in operation order.
Function
runNumberAllocator(n: int, operations: String[], values: int[]) → int[]Examples
Example 1
n = 3operations = ["assign","assign","release","assign","assign"]values = [-1,-1,0,-1,-1]return = [0,1,0,2]The first two assignments reserve 0 and 1. Releasing 0 makes it the smallest available number again, so the next assignment reuses it before assigning 2.
Example 2
n = 2operations = ["assign","assign","assign","release","assign"]values = [-1,-1,-1,1,-1]return = [0,1,-1,1]After 0 and 1 are assigned, the allocator is exhausted and returns -1. Releasing 1 lets the final assignment return 1.
Constraints
1 <= n <= 10^61 <= operations.length <= 2 * 10^5values.length == operations.length- Each operation is exactly
assignorrelease. - For
assign, the corresponding value is-1. - For
release,0 <= values[i] < nand that number is currently assigned.