-
Notifications
You must be signed in to change notification settings - Fork 9
/
Solution.cpp
32 lines (27 loc) · 963 Bytes
/
Solution.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
// https://leetcode.com/problems/design-authentication-manager
class AuthenticationManager {
public:
int timeToLive;
map<string, int> endtime;
set<pair<int, string>> toremove;
AuthenticationManager(int timeToLive) : timeToLive(timeToLive) {}
void generate(string tokenId, int currentTime) {
endtime[tokenId] = currentTime + timeToLive;
toremove.insert({endtime[tokenId], tokenId});
}
void renew(string tokenId, int currentTime) {
if (endtime.count(tokenId) and endtime[tokenId] > currentTime) {
toremove.erase({endtime[tokenId], tokenId});
endtime[tokenId] = currentTime + timeToLive;
toremove.insert({endtime[tokenId], tokenId});
}
}
int countUnexpiredTokens(int currentTime) {
while (!toremove.empty() and toremove.begin()->first <= currentTime) {
auto [t, token] = *toremove.begin();
toremove.erase(toremove.begin());
endtime.erase(token);
}
return endtime.size();
}
};