Design Phone Directory
Below is a simple and straightforward implementation of a phone directory as described in the given problem statement.
|
|
Explanation:
Initialization (
__init__
method): This method initializes the directory with a set of available numbers from0
tomaxNumbers-1
and an empty set of used numbers.Get a Number (
get
method): This method provides an available number. If there’s any available number, it removes one from the available set, adds it to the used set, and returns it. If no numbers are available, it returns-1
.Check a Number (
check
method): This method checks whether a given number is available. If the number is in the available set, it returnsTrue
; otherwise, it returnsFalse
.Release a Number (
release
method): This method recycles a number by removing it from the used set and adding it back to the available set. If the number is not in the used set, this method does nothing.
The solution works in constant time for all operations, and the space complexity is (O(\text{{maxNumbers}})), where (\text{{maxNumbers}}) is the maximum number of slots in the phone directory.
|
|