forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_379.java
60 lines (53 loc) · 1.67 KB
/
_379.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.fishercoder.solutions;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
public class _379 {
public static class Solution1 {
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);
}
}
}
}
}