Design Phone Directory — LeetCode 379 Python Solution
MediumLeetCode PremiumDesignQueueArrayHash TableLinked List
- Problem
- #379
- Pattern
- Linked List
- Reading time
- 4 min
- Source
- leetcode.com
The problem
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.
Example
- Input
- ["PhoneDirectory", "get", "get", "check", "get", "check", "release", "check"]
- Output
- [null, 0, 1, true, 2, false, null, true]
- Explanation
- PhoneDirectory phoneDirectory = new PhoneDirectory(3);
Python solution
Python
class PhoneDirectory:
def __init__(self, maxNumbers: int):
self.available = set(range(maxNumbers))
def get(self) -> int:
if not self.available:
return -1
return self.available.pop()
def check(self, number: int) -> bool:
return number in self.available
def release(self, number: int) -> None:
self.available.add(number)
# Your PhoneDirectory object will be instantiated and called as such:
# obj = PhoneDirectory(maxNumbers)
# param_1 = obj.get()
# param_2 = obj.check(number)
# obj.release(number)Complexity
| Measure | Complexity |
|---|---|
| Time | O(1) |
| Space | O(n), where n is the value of `maxNumbers` auxiliary |
Pattern: Linked List
Rewire pointers in place, with a dummy head and a saved next to keep it safe. LeetCode 379. Design Phone Directory is filed here because LeetCode tags it Linked List, which is the vocabulary this hub collects.
The linked list guide has the Python template for the pattern and the 75 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 379. Design Phone Directory?
- LeetCode 379. Design Phone Directory is rated Medium on LeetCode.
- What is the time complexity of LeetCode 379. Design Phone Directory?
- The Python solution on this page runs in O(1).
- What is the space complexity of LeetCode 379. Design Phone Directory?
- The Python solution on this page uses O(n), where n is the value of `maxNumbers` auxiliary space.
- What topics does LeetCode 379. Design Phone Directory cover?
- LeetCode 379. Design Phone Directory is tagged Design, Queue, Array, Hash Table and Linked List on LeetCode.
- Is LeetCode 379. Design Phone Directory a premium problem?
- Yes. LeetCode 379. Design Phone Directory is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.