-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathdesign-phone-directory.cc
42 lines (38 loc) · 1.13 KB
/
design-phone-directory.cc
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
// Design Phone Directory
class PhoneDirectory {
vector<bool> used;
vector<int> a;
int top = 0;
public:
/** Initialize your data structure here
@param maxNumbers - The maximum numbers that can be stored in the phone directory. */
PhoneDirectory(int maxNumbers) : a(maxNumbers), used(maxNumbers, false) {
iota(a.begin(), a.end(), 0);
}
/** Provide a number which is not assigned to anyone.
@return - Return an available number. Return -1 if none is available. */
int get() {
if (top < a.size()) {
used[a[top]] = true;
return a[top++];
}
return -1;
}
/** Check if a number is available or not. */
bool check(int number) {
return 0 <= number && number < a.size() && ! used[number];
}
/** Recycle or release a number. */
void release(int number) {
if (0 <= number && number < a.size() && used[number]) {
used[a[--top] = number] = false;
}
}
};
/**
* 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);
*/