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)


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