-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathdesign-phone-directory.js
89 lines (83 loc) · 2.02 KB
/
design-phone-directory.js
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
/**
* 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);
*/
class PhoneDirectory {
/**
* Initialize your data structure here
@param maxNumbers - The maximum numbers that can be stored in the phone directory.
* @param {number} maxNumbers
*/
constructor(maxNumbers) {
this.pos = 0;
this.nums = [];
for (let i = 0; i < maxNumbers; i++) {
this.nums[i] = (i + 1) % maxNumbers;
}
}
/**
* Provide a number which is not assigned to anyone.
@return - Return an available number. Return -1 if none is available.
* @return {number}
*/
get() {
if (this.nums[this.pos] === -1) {
return -1;
}
const res = this.pos;
this.pos = this.nums[res];
this.nums[res] = -1;
return res;
}
/**
* Check if a number is available or not.
* @param {number} number
* @return {boolean}
*/
check(number) {
return this.nums[number] !== -1;
}
/**
* Recycle or release a number.
* @param {number} number
* @return {void}
*/
release(number) {
if (this.nums[number] !== -1) {
return;
}
this.nums[number] = this.pos;
this.pos = number;
}
}
export { PhoneDirectory };