-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_1797_AuthenticationManager.cc
More file actions
47 lines (41 loc) · 1022 Bytes
/
Copy pathProblem_1797_AuthenticationManager.cc
File metadata and controls
47 lines (41 loc) · 1022 Bytes
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
#include <unordered_map>
#include <vector>
#include "UnitTest.h"
using namespace std;
class AuthenticationManager
{
public:
AuthenticationManager(int timeToLive) { ttl = timeToLive; }
void generate(string tokenId, int currentTime) { items.emplace(tokenId, currentTime); }
void renew(string tokenId, int currentTime)
{
if (items.count(tokenId))
{
if (currentTime < items[tokenId] + ttl)
{
items[tokenId] = currentTime;
}
}
}
int countUnexpiredTokens(int currentTime)
{
int ans = 0;
for (auto &[tokenId, time] : items)
{
if (time + ttl > currentTime)
{
ans++;
}
}
return ans;
}
int ttl;
unordered_map<string, int> items;
};
/**
* Your AuthenticationManager object will be instantiated and called as such:
* AuthenticationManager* obj = new AuthenticationManager(timeToLive);
* obj->generate(tokenId,currentTime);
* obj->renew(tokenId,currentTime);
* int param_3 = obj->countUnexpiredTokens(currentTime);
*/