leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1797.cpp (869B)


0 class AuthenticationManager { 1 const int ttl; 2 mutable unordered_map<string, int> expire; 3 4 public: 5 AuthenticationManager(int timeToLive) : ttl(timeToLive) {} 6 7 void generate(const string &tokenId, int currentTime) const { 8 expire.emplace(tokenId, currentTime + ttl); 9 } 10 11 void renew(const string &tokenId, int currentTime) const { 12 auto it = expire.find(tokenId); 13 if (it == end(expire)) return; 14 if (it->second <= currentTime) { 15 expire.erase(it); 16 return; 17 } 18 it->second = currentTime + ttl; 19 } 20 21 int countUnexpiredTokens(int currentTime) const { 22 for (auto it = begin(expire); it != end(expire);) { 23 if (it->second <= currentTime) 24 it = expire.erase(it); 25 else 26 it++; 27 } 28 return size(expire); 29 } 30 };