Skip to content

Commit c782f61

Browse files
design phone directory
1 parent 2488960 commit c782f61

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package medium;
2+
3+
import java.util.HashSet;
4+
import java.util.LinkedList;
5+
import java.util.Queue;
6+
import java.util.Set;
7+
8+
public class DesignPhoneDirectory {
9+
10+
private class PhoneDirectory {
11+
private Queue<Integer> phoneDir;
12+
private Set<Integer> used;
13+
/**
14+
* Initialize your data structure here
15+
*
16+
* @param maxNumbers
17+
* - The maximum numbers that can be stored in the phone directory.
18+
*/
19+
public PhoneDirectory(int maxNumbers) {
20+
phoneDir = new LinkedList();
21+
int number = 0;
22+
while (maxNumbers-- > 0){
23+
phoneDir.add(number++);
24+
}
25+
used = new HashSet();
26+
}
27+
28+
/**
29+
* Provide a number which is not assigned to anyone.
30+
*
31+
* @return - Return an available number. Return -1 if none is available.
32+
*/
33+
public int get() {
34+
if (phoneDir.peek() == null) return -1;
35+
int newNumber = phoneDir.poll();
36+
used.add(newNumber);
37+
return newNumber;
38+
}
39+
40+
/** Check if a number is available or not. */
41+
public boolean check(int number) {
42+
return !used.contains(number);
43+
}
44+
45+
/** Recycle or release a number. */
46+
public void release(int number) {
47+
if (used.remove(number)) {
48+
phoneDir.add(number);
49+
}
50+
}
51+
}
52+
53+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
|388|[Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/)|[Solution](../../blob/master/MEDIUM/src/medium/LongestAbsoluteFilePath.java)| O(n)|O(d) | Medium| Stack
3030
|387|[First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/)|[Solution](../../blob/master/EASY/src/easy/FirstUniqueCharacterinaString.java)| O(n)|O(n) | Easy| HashMap
3131
|386|[Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/)|[Solution](../../blob/master/MEDIUM/src/medium/LexicographicalNumbers.java)| O(n)|O(1) | Medium|
32+
|379|[Design Phone Directory](https://leetcode.com/problems/design-phone-directory/)|[Solution](../../blob/master/MEDIUM/src/medium/DesignPhoneDirectory.java)| O(1)|O(n) | Medium|
3233
|374|[Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/)|[Solution](../../blob/master/EASY/src/easy/GuessNumberHigherorLower.java)| O(logn)|O(1) | Easy| Binary Search
3334
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/)|[Solution](../../blob/master/EASY/src/easy/IntersectionOfTwoArraysII.java)| O(m+n)|O((m+n)) could be optimized | Easy| HashMap, Binary Search
3435
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/)|[Solution](../../blob/master/EASY/src/easy/IntersectionOfTwoArrays.java)| O(m+n)|O(min(m,n)) | Easy| Two Pointers, Binary Search

0 commit comments

Comments
 (0)