-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.ts
38 lines (34 loc) · 906 Bytes
/
solution.ts
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
class PhoneDirectory {
used = new Set<number>();
candidate:number[] = [];
constructor (maxNumbers: number) {
for (let i = maxNumbers - 1; i > -1; i--) {
this.candidate.push(i);
}
}
get (): number {
if (this.candidate.length === 0) {
return -1;
}
const result = this.candidate.pop();
this.used.add(result);
return result;
}
check (number: number): boolean {
return !this.used.has(number);
}
release (number: number): void {
if (!this.used.has(number)) {
return;
}
this.used.delete(number);
this.candidate.push(number);
}
}
/**
* Your PhoneDirectory object will be instantiated and called as such:
* var obj = new PhoneDirectory(maxNumbers)
* var param_1 = obj.get()
* var param_2 = obj.check(number)
* obj.release(number)
*/