-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.ts
38 lines (33 loc) · 1.05 KB
/
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 AuthenticationManager {
private timeToLive: number;
private map: Map<string, number>;
constructor(timeToLive: number) {
this.timeToLive = timeToLive;
this.map = new Map<string, number>();
}
generate(tokenId: string, currentTime: number): void {
this.map.set(tokenId, currentTime + this.timeToLive);
}
renew(tokenId: string, currentTime: number): void {
if ((this.map.get(tokenId) ?? 0) <= currentTime) {
return;
}
this.map.set(tokenId, currentTime + this.timeToLive);
}
countUnexpiredTokens(currentTime: number): number {
let res = 0;
for (const time of this.map.values()) {
if (time > currentTime) {
res++;
}
}
return res;
}
}
/**
* Your AuthenticationManager object will be instantiated and called as such:
* var obj = new AuthenticationManager(timeToLive)
* obj.generate(tokenId,currentTime)
* obj.renew(tokenId,currentTime)
* var param_3 = obj.countUnexpiredTokens(currentTime)
*/