package com.fishercoder.solutions;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

/**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);
 */
public class _379 {

    private class PhoneDirectory {
        private Queue<Integer> phoneDir;
        private Set<Integer> used;

        /**
         * Initialize your data structure here
         *
         * @param maxNumbers - The maximum numbers that can be stored in the phone directory.
         */
        public PhoneDirectory(int maxNumbers) {
            phoneDir = new LinkedList();
            int number = 0;
            while (maxNumbers-- > 0) {
                phoneDir.add(number++);
            }
            used = new HashSet();
        }

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

        /**
         * 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.remove(number)) {
                phoneDir.add(number);
            }
        }
    }

}
@@ -134,16 +134,54 @@
 et;%0A%0A/**
+%0A * 379. Design Phone Directory%0A *%0A * 
 Design a
@@ -1026,16 +1026,56 @@
 _379 %7B%0A%0A
+    public static class Solution1 %7B%0A    
     priv
@@ -1101,32 +1101,36 @@
 ctory %7B%0A        
+  
+  
 private Queue%3CIn
@@ -1146,32 +1146,36 @@
 oneDir;%0A        
+    
 private Set%3CInte
@@ -1186,32 +1186,36 @@
  used;%0A%0A        
+ 
+   
 /**%0A         * I
@@ -1211,16 +1211,20 @@
         
+   
+ 
 * Initia
@@ -1254,34 +1254,42 @@
 e here%0A         
-*%0A
+    *%0A    
          * @para
@@ -1367,32 +1367,36 @@
 ctory.%0A         
+    
 */%0A        publi
@@ -1386,24 +1386,28 @@
  */%0A        
+ 
+   
 public Phone
@@ -1430,24 +1430,28 @@
 xNumbers) %7B%0A
+    
             
@@ -1487,24 +1487,28 @@
             
+    
 int number =
@@ -1523,16 +1523,20 @@
         
+  
+  
 while (m
@@ -1550,24 +1550,28 @@
 rs-- %3E 0) %7B%0A
+    
             
@@ -1602,34 +1602,42 @@
 +);%0A            
-%7D%0A
+    %7D%0A    
             used
@@ -1654,32 +1654,36 @@
 hSet();%0A        
+    
 %7D%0A%0A        /**%0A 
@@ -1669,36 +1669,44 @@
      %7D%0A%0A        
+  
+  
 /**%0A
+    
          * Provi
@@ -1751,34 +1751,42 @@
 nyone.%0A         
+ 
-*%0A
+   *%0A    
          * @retu
@@ -1855,35 +1855,42 @@
 e.%0A         
-*/%0A
+    */%0A   
         
 public int g
@@ -1873,24 +1873,25 @@
 %0A           
+ 
 public int g
@@ -1893,24 +1893,28 @@
 int get() %7B%0A
+    
             
@@ -1956,24 +1956,28 @@
             
+    
 return -1;%0A 
@@ -1979,32 +1979,36 @@
 -1;%0A            
+    
 %7D%0A            in
@@ -2001,24 +2001,28 @@
             
+ 
+   
 int newNumbe
@@ -2038,24 +2038,28 @@
 Dir.poll();%0A
+    
             
@@ -2087,24 +2087,28 @@
             
+    
 return newNu
@@ -2113,35 +2113,43 @@
 Number;%0A        
+   
+ 
 %7D%0A%0A
+    
         /**%0A    
@@ -2136,32 +2136,36 @@
             /**%0A
+    
          * Check
@@ -2198,16 +2198,20 @@
 or not.%0A
+    
         
@@ -2218,24 +2218,28 @@
  */%0A        
+    
 public boole
@@ -2269,24 +2269,28 @@
             
+   
+ 
 return !used
@@ -2308,32 +2308,36 @@
 umber);%0A        
+    
 %7D%0A%0A        /**%0A 
@@ -2323,36 +2323,44 @@
      %7D%0A%0A        
+  
+  
 /**%0A
+    
          * Recyc
@@ -2392,27 +2392,34 @@
         
-*/%0A
+    */%0A   
         
 public v
@@ -2406,24 +2406,25 @@
 %0A           
+ 
 public void 
@@ -2453,24 +2453,28 @@
             
+    
 if (used.rem
@@ -2496,32 +2496,36 @@
                 
+  
+  
 phoneDir.add(num
@@ -2522,32 +2522,50 @@
 ir.add(number);%0A
+                %7D%0A
             %7D%0A  
@@ -2582,6 +2582,6 @@
   %7D%0A
-%0A
 %7D
+%0A

refactor 379