Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AuthenticationManager { // https://leetcode.com/problems/design-authentication-manager/
- private:
- unordered_map<string, int> users;
- multiset<int> times;
- int ttl = 0;
- public:
- AuthenticationManager(int timeToLive) {
- ttl = timeToLive;
- }
- void generate(string tokenId, int currentTime) {
- this->users[tokenId] = currentTime + this->ttl;
- this->times.insert(currentTime + this->ttl);
- }
- void renew(string tokenId, int currentTime) {
- auto prev = this->users[tokenId];
- this->users[tokenId] = currentTime + this->ttl;
- this->times.erase(this->times.find(prev));
- }
- int countUnexpiredTokens(int currentTime) {
- auto range = this->times.upper_bound(currentTime);
- return distance(this->times.begin(), range) - 1;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement