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.
class PhoneDirectory {
    private int min;
    private int max;
    private HashSet<Integer> set;

    /** Initialize your data structure here
        @param maxNumbers - The maximum numbers that can be stored in the phone directory. */
    public PhoneDirectory(int maxNumbers) {
        min = 0;
        max = maxNumbers;
        set = new HashSet<Integer>();
    }

    /** Provide a number which is not assigned to anyone.
        @return - Return an available number. Return -1 if none is available. */
    public int get() {
        if(min == max)
            return -1;

        int res = min;
        set.add(res);
        while(set.contains(min) && min < max)
            min++;

        return res;
    }

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

    /** Recycle or release a number. */
    public void release(int number) {
        set.remove(number);
        min = Math.min(min, 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);
 */

results matching ""

    No results matching ""