-
Notifications
You must be signed in to change notification settings - Fork 28
/
Design_Phone_Directory.cpp
48 lines (44 loc) · 1.44 KB
/
Design_Phone_Directory.cpp
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
// There must be some problem in OJ. Got TLE with Linkedlist, unordered_set for no reason :/
class PhoneDirectory {
int indx;
vector<int> available;
vector<bool> isAvailable;
public:
/** Initialize your data structure here
@param maxNumbers - The maximum numbers that can be stored in the phone directory. */
PhoneDirectory(int maxNumbers) {
indx = maxNumbers;
available = vector<int>(maxNumbers);
isAvailable = vector<bool>(maxNumbers);
for (int i = 0; i< maxNumbers; i++) {
available[i] = i;
isAvailable[i] = true;
}
}
/** Provide a number which is not assigned to anyone.
@return - Return an available number. Return -1 if none is available. */
int get() {
if (!indx) return -1;
int number = available[--indx];
isAvailable[number] = false;
return number;
}
/** Check if a number is available or not. */
bool check(int number) {
return isAvailable[number];
}
/** Recycle or release a number. */
void release(int number) {
if (!isAvailable[number]) {
isAvailable[number] = true;
available[indx++] = number;
}
}
};
/**
* Your PhoneDirectory object will be instantiated and called as such:
* PhoneDirectory obj = new PhoneDirectory(maxNumbers);
* int param_1 = obj.get();
* bool param_2 = obj.check(number);
* obj.release(number);
*/