-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdesign_authentication_manager.cpp
39 lines (33 loc) · 1.1 KB
/
design_authentication_manager.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
// https://leetcode.com/problems/design-authentication-manager/
class AuthenticationManager {
public:
int lifeTime;
unordered_map <string, int> tokens;
AuthenticationManager(int timeToLive) {
lifeTime = timeToLive;
}
void generate(string tokenId, int currentTime) {
tokens[tokenId] = currentTime;
}
void renew(string tokenId, int currentTime) {
if(tokens.find(tokenId) != tokens.end() && (tokens[tokenId] + lifeTime) > currentTime) {
tokens[tokenId] = currentTime;
}
}
int countUnexpiredTokens(int currentTime) {
int count = 0;
for(auto it=tokens.begin(); it!=tokens.end(); it++) {
if((it->second + lifeTime) > currentTime) {
count++;
}
}
return count;
}
};
/**
* 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);
*/