-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesignAuthenticationManager.cpp
More file actions
48 lines (40 loc) · 1.22 KB
/
designAuthenticationManager.cpp
File metadata and controls
48 lines (40 loc) · 1.22 KB
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
48
// Source: https://leetcode.com/problems/design-authentication-manager/
// Author: Miao Zhang
// Date: 2021-06-08
class AuthenticationManager {
public:
AuthenticationManager(int timeToLive) : ttl_(timeToLive) {
}
void generate(string tokenId, int currentTime) {
clear(currentTime);
tokens_[tokenId] = currentTime + ttl_;
}
void renew(string tokenId, int currentTime) {
clear(currentTime);
if (!tokens_.count(tokenId)) return;
tokens_[tokenId] = currentTime + ttl_;
}
int countUnexpiredTokens(int currentTime) {
clear(currentTime);
return tokens_.size();
}
private:
unordered_map<string, int> tokens_;
int ttl_;
void clear(int currentTime) {
vector<string> ids;
for (auto& [id, t]: tokens_) {
if (t <= currentTime) ids.push_back(id);
}
for (string& id: ids) {
tokens_.erase(id);
}
}
};
/**
* 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);
*/