Skip to content

Latest commit

 

History

History

379

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Design a Phone Directory which supports the following operations:

  1. get: Provide a number which is not assigned to anyone.
  2. check: Check if a number is available or not.
  3. release: Recycle or release a number.

Example:

// Init a phone directory containing a total of 3 numbers: 0, 1, and 2.
PhoneDirectory directory = new PhoneDirectory(3);

// It can return any available phone number. Here we assume it returns 0. directory.get();

// Assume it returns 1. directory.get();

// The number 2 is available, so return true. directory.check(2);

// It returns 2, the only number that is left. directory.get();

// The number 2 is no longer available, so return false. directory.check(2);

// Release number 2 back to the pool. directory.release(2);

// Number 2 is available again, return true. directory.check(2);

Companies:
Dropbox, Google

Related Topics:
Linked List, Design

Solution 1.

// OJ: https://leetcode.com/problems/design-phone-directory/
// Author: github.com/lzl124631x
// Time: O(1)
// Space: O(N)
class PhoneDirectory {
private:
    unordered_set<int> pool;
public:
    PhoneDirectory(int maxNumbers) {
        while (maxNumbers--) pool.insert(maxNumbers);
    }
    int get() {
        if (pool.empty()) return -1;
        int ans = *pool.begin();
        pool.erase(pool.begin());
        return ans;
    }
    bool check(int number) {
        return pool.find(number) != pool.end();
    }
    void release(int number) {
        if (check(number)) return;
        pool.insert(number);
    }
};