10379. Design Phone Directory

Create a NumberRegistry system that supports the following operations:

  1. fetch: Retrieve the minimum unused number from the registry.
  2. isAvailable: Check if a specific number can be assigned.
  3. returnNumber: Make a previously assigned number available again.

Examples

// Initialize a number registry with 4 numbers: 0, 1, 2, and 3.
NumberRegistry registry = new NumberRegistry(4);

// May return any unused number. Suppose it returns 1.
registry.fetch();

// Suppose it returns 0.
registry.fetch();

// Number 2 is still available, so this returns true.
registry.isAvailable(2);

// Only number 3 is left, so this call returns 3.
registry.fetch();

// Number 2 is still available, so this returns true.
registry.isAvailable(2);

// Release the number 2 back to the registry.
registry.returnNumber(2);

// Now, number 2 is available again, so this returns true.
registry.isAvailable(2);

Constraints

  • All numbers in the registry are in the range 0 to n - 1 where 1 ≤ n ≤ 9000.
  • Each number is unique and can be assigned only once until released.
  • Operations fetch, isAvailable, and returnNumber will be called at most 30,000 times in total.




Please use Laptop/Desktop or any other large screen to add/edit code.