Skip to content

379 Design Phone Directory

Problem:

Design a Phone Directory which supports the following operations:

get: Provide a number which is not assigned to anyone. check: Check if a number is available or not. 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);

Solutions:

public class PhoneDirectory {
    private HashSet<Integer> used;
    private Queue<Integer> available;
    /** Initialize your data structure here
        @param maxNumbers - The maximum numbers that can be stored in the phone directory. */
    public PhoneDirectory(int maxNumbers) {
        available = new LinkedList<Integer>();
        used = new HashSet<Integer>();
        for (int i = 0; i < maxNumbers; i ++) {
            available.add(i);
        }
    }

    /** Provide a number which is not assigned to anyone.
        @return - Return an available number. Return -1 if none is available. */
    public int get() {
        if (available.isEmpty()) {
            return -1;
        }
        int next = available.poll();
        used.add(next);
        return next;
    }

    /** Check if a number is available or not. */
    public boolean check(int number) {
        return !used.contains(number);
    }

    /** Recycle or release a number. */
    public void release(int number) {
        if (used.contains(number)) {
           used.remove((Integer) number);
            available.add(number); 
        }
    }
}

/**
 * Your PhoneDirectory object will be instantiated and called as such:
 * PhoneDirectory obj = new PhoneDirectory(maxNumbers);
 * int param_1 = obj.get();
 * boolean param_2 = obj.check(number);
 * obj.release(number);
 */
public class PhoneDirectory {
    private int[] numbers;
    private boolean[] used;
    private int max;
    private int next;
    /** Initialize your data structure here
        @param maxNumbers - The maximum numbers that can be stored in the phone directory. */
    public PhoneDirectory(int maxNumbers) {
        max = maxNumbers;
        numbers = new int[max];
        for (int i = 0; i < max; i ++) {
            numbers[i] = i;
        }
        used = new boolean[max];
        next = 0;
    }

    /** Provide a number which is not assigned to anyone.
        @return - Return an available number. Return -1 if none is available. */
    public int get() {
        if (next < max) {
            int result = numbers[next ++];
            used[result] = true;
            return result;
        }
        return -1;
    }

    /** Check if a number is available or not. */
    public boolean check(int number) {
        return number < max && number >= 0 && !used[number];
    }

    /** Recycle or release a number. */
    public void release(int number) {
        if (used[number]) {
            next --;
            numbers[next] = number;
            used[number] = false;
        }
    }
}

/**
 * Your PhoneDirectory object will be instantiated and called as such:
 * PhoneDirectory obj = new PhoneDirectory(maxNumbers);
 * int param_1 = obj.get();
 * boolean param_2 = obj.check(number);
 * obj.release(number);
 */