Design Phone Directory Problem
Description
LeetCode Problem 379.
Design a phone directory that initially has maxNumbers empty slots that can store numbers. The directory should store numbers, check if a certain slot is empty or not, and empty a given slot.
Implement the PhoneDirectory class:
PhoneDirectory(int maxNumbers)Initializes the phone directory with the number of available slotsmaxNumbers.int get()Provides a number that is not assigned to anyone. Returns-1if no number is available.bool check(int number)Returnstrueif the slotnumberis available andfalseotherwise.void release(int number)Recycles or releases the slotnumber.
Example 1:
Input
["PhoneDirectory", "get", "get", "check", "get", "check", "release", "check"]
[[3], [], [], [2], [], [2], [2], [2]]
Output
[null, 0, 1, true, 2, false, null, true]
Explanation
PhoneDirectory phoneDirectory = new PhoneDirectory(3);
phoneDirectory.get(); // It can return any available phone number. Here we assume it returns 0.
phoneDirectory.get(); // Assume it returns 1.
phoneDirectory.check(2); // The number 2 is available, so return true.
phoneDirectory.get(); // It returns 2, the only number that is left.
phoneDirectory.check(2); // The number 2 is no longer available, so return false.
phoneDirectory.release(2); // Release number 2 back to the pool.
phoneDirectory.check(2); // Number 2 is available again, return true.
Constraints:
1 <= maxNumbers <= 10^40 <= number < maxNumbers- At most
2 * 10^4calls will be made toget,check, andrelease.
Solution
Approach: Stack of Free Numbers
We need three things to be fast: hand out some free number, tell whether one given number is free, and put a number back. Two simple structures cover them:
available[number]: a boolean array,truewhile nobody holds the number. This answerscheckdirectly.free: a stack holding exactly the numbers whoseavailableflag istrue.getpops from it, so it never has to search for a free slot.
At the start every number is free, so free holds 0 .. maxNumbers - 1 (pushed in reverse so that get hands out 0, 1, 2, ... first, though any order is allowed).
get: iffreeis empty, return-1. Otherwise pop a number, mark it taken and return it.check(number): returnavailable[number].release(number): if the number is taken, mark it free and push it back onfree.
The tricky case is release of a number that is already free (the constraints allow it). Pushing it again would put it on the stack twice, and two later get calls could hand out the same number. Checking the flag first keeps the stack and the array in step.
Complexity: O(1) time for every call, after O(maxNumbers) setup. O(maxNumbers) space.
class PhoneDirectory:
def __init__(self, maxNumbers: int):
self.free = list(range(maxNumbers - 1, -1, -1)) # numbers nobody holds, used as a stack
self.available = [True] * maxNumbers
def get(self) -> int:
if not self.free:
return -1
number = self.free.pop()
self.available[number] = False
return number
def check(self, number: int) -> bool:
return self.available[number]
def release(self, number: int) -> None:
if not self.available[number]: # releasing a free number must not add it twice
self.available[number] = True
self.free.append(number)