Advertisement
CyberN00b

Untitled

Nov 28th, 2022
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. class AuthenticationManager { // https://leetcode.com/problems/design-authentication-manager/
  2. private:
  3. unordered_map<string, int> users;
  4. multiset<int> times;
  5. int ttl = 0;
  6. public:
  7. AuthenticationManager(int timeToLive) {
  8. ttl = timeToLive;
  9. }
  10.  
  11. void generate(string tokenId, int currentTime) {
  12. this->users[tokenId] = currentTime + this->ttl;
  13. this->times.insert(currentTime + this->ttl);
  14. }
  15.  
  16. void renew(string tokenId, int currentTime) {
  17. auto prev = this->users[tokenId];
  18. this->users[tokenId] = currentTime + this->ttl;
  19. this->times.erase(this->times.find(prev));
  20. }
  21.  
  22. int countUnexpiredTokens(int currentTime) {
  23. auto range = this->times.upper_bound(currentTime);
  24. return distance(this->times.begin(), range) - 1;
  25. }
  26. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement