-
Notifications
You must be signed in to change notification settings - Fork 43
/
DesignPhoneDirectory.java
333 lines (300 loc) · 10.1 KB
/
DesignPhoneDirectory.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package LeetCodeJava.Design;
// https://leetcode.com/problems/design-phone-directory/description/
// https://leetcode.ca/all/379.html
import java.util.*;
/**
* 379. Design Phone Directory
* 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);
* Difficulty:
* Medium
* Lock:
* Prime
* Company:
* Dropbox Google Microsoft
*/
public class DesignPhoneDirectory {
/**
* 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);
*/
// V0
// TODO: validate below
// class PhoneDirectory {
//
// // attr
// //int assignedCnt;
// /**
// * assignedPhone : {assigned_number: 1}
// *
// */
// Map<Integer, Integer> assignedPhone;
// int maxNumbers;
// //Random random;
//
// /** Initialize your data structure here
// @param maxNumbers - The maximum numbers that can be stored in the phone directory. */
// public PhoneDirectory(int maxNumbers) {
// //this.assignedCnt = 0;
// this.assignedPhone = new HashMap<>();
// this.maxNumbers = maxNumbers;
// //this.random = new Random();
// }
//
// /** Provide a number which is not assigned to anyone.
// @return - Return an available number. Return -1 if none is available. */
// public int get() {
// if(this.assignedPhone.isEmpty()){
// return -1;
// }
// int key = -1;
// for (int i = 0; i < this.maxNumbers; i++){
// if(!this.assignedPhone.containsKey(i)){
// key = i;
// break;
// }
// }
// if(key != -1){
// this.assignedPhone.put(key, 1);
// }
// return key;
// }
//
// /** Check if a number is available or not. */
// public boolean check(int number) {
// return this.assignedPhone.containsKey(number);
// }
//
// /** Recycle or release a number. */
// public void release(int number) {
// if(this.assignedPhone.containsKey(number)){
// this.assignedPhone.remove(number);
// }
// }
// }
// V0-1
// IDEA: queue + set (fixed by gpt)
/**
* Optimized Solution:
*
* To efficiently solve the problem, we can use:
* 1. A queue to store the available numbers.
* 2. A set to quickly check if a number is assigned or not.
*
* Here’s a corrected and efficient implementation:
*
*
* Explanation:
* 1. Initialization:
* • A Queue<Integer> (availableNumbers) holds all available numbers initially, starting from 0 to maxNumbers - 1.
* • A Set<Integer> (assignedNumbers) keeps track of all assigned numbers for efficient lookups.
*
* 2. get():
* • If the queue of available numbers is not empty, it dequeues the next available number, adds it to the set of assigned numbers, and returns it.
* • If no numbers are available, it returns -1.
*
* 3. check(int number):
* • Returns true if the number is not in the set of assigned numbers, indicating it is available.
* • Handles out-of-bound numbers by returning false.
*
* 4. release(int number):
* • Removes the number from the set of assigned numbers and enqueues it back into the queue of available numbers.
*
*/
class PhoneDirectory_0_1 {
private Queue<Integer> availableNumbers;
private Set<Integer> assignedNumbers;
private int maxNumbers;
/** Initialize your data structure here. */
public PhoneDirectory_0_1(int maxNumbers) {
this.maxNumbers = maxNumbers;
this.availableNumbers = new LinkedList<>();
this.assignedNumbers = new HashSet<>();
// Initialize the queue with all numbers
for (int i = 0; i < maxNumbers; i++) {
this.availableNumbers.offer(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 (availableNumbers.isEmpty()) {
return -1; // No available numbers
}
int number = availableNumbers.poll();
assignedNumbers.add(number);
return number;
}
/** Check if a number is available or not. */
public boolean check(int number) {
if (number < 0 || number >= maxNumbers) {
return false; // Out of bounds
}
return !assignedNumbers.contains(number);
}
/** Recycle or release a number. */
public void release(int number) {
if (assignedNumbers.contains(number)) {
assignedNumbers.remove(number);
availableNumbers.offer(number); // Add back to the pool of available numbers
}
}
}
// V1-1
// https://leetcode.ca/2016-12-13-379-Design-Phone-Directory/
class PhoneDirectory_1_1 {
int maxNumbers;
boolean[] available;
Queue<Integer> unused;
/** Initialize your data structure here
@param maxNumbers - The maximum numbers that can be stored in the phone directory. */
public PhoneDirectory_1_1(int maxNumbers) {
this.maxNumbers = maxNumbers;
available = new boolean[maxNumbers];
unused = new LinkedList<Integer>();
for (int i = 0; i < maxNumbers; i++) {
available[i] = true;
unused.offer(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 (unused.isEmpty())
return -1;
else {
int next = unused.poll();
available[next] = false;
return next;
}
}
/** Check if a number is available or not. */
public boolean check(int number) {
if (number < 0 || number >= maxNumbers)
return false;
else
return available[number];
}
/** Recycle or release a number. */
public void release(int number) {
if (!available[number]) {
unused.offer(number);
available[number] = true;
}
}
}
// V1-2
// https://leetcode.ca/2016-12-13-379-Design-Phone-Directory/
public class PhoneDirectory_1_2 {
int max;
HashSet<Integer> set; // assigned number
LinkedList<Integer> queue; // available number
/**
* Initialize your data structure here
*
* @param maxNumbers - The maximum numbers that can be stored in the phone directory.
*/
public PhoneDirectory_1_2(int maxNumbers) {
set = new HashSet<Integer>();
queue = new LinkedList<Integer>();
for (int i = 0; i < maxNumbers; i++) {
queue.offer(i);
}
max = maxNumbers - 1;
}
/**
* Provide a number which is not assigned to anyone.
*
* @return - Return an available number. Return -1 if none is available.
*/
public int get() {
if (queue.isEmpty()) {
return -1;
}
int e = queue.poll();
set.add(e);
return e;
}
/**
* 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) {
if (set.contains(number)) {
set.remove(number);
queue.offer(number);
}
}
}
// V1-3
// https://leetcode.ca/2016-12-13-379-Design-Phone-Directory/
class PhoneDirectory_1_3 {
private boolean[] provided;
/**
Initialize your data structure here
@param maxNumbers - The maximum numbers that can be stored in the phone directory.
*/
public PhoneDirectory_1_3(int maxNumbers) {
provided = new boolean[maxNumbers];
}
/**
Provide a number which is not assigned to anyone.
@return - Return an available number. Return -1 if none is available.
*/
public int get() {
for (int i = 0; i < provided.length; ++i) {
if (!provided[i]) {
provided[i] = true;
return i;
}
}
return -1;
}
/** Check if a number is available or not. */
public boolean check(int number) {
return !provided[number];
}
/** Recycle or release a number. */
public void release(int number) {
provided[number] = false;
}
}
// V2
}