{"id":8252,"date":"2021-03-20T09:28:53","date_gmt":"2021-03-20T16:28:53","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8252"},"modified":"2021-03-20T09:31:50","modified_gmt":"2021-03-20T16:31:50","slug":"leetcode-1797-design-authentication-manager","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1797-design-authentication-manager\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1797. Design Authentication Manager"},"content":{"rendered":"\n<p>There is an authentication system that works with authentication tokens. For each session, the user will receive a new authentication token that will expire&nbsp;<code>timeToLive<\/code>&nbsp;seconds after the&nbsp;<code>currentTime<\/code>. If the token is renewed, the expiry time will be&nbsp;<strong>extended<\/strong>&nbsp;to expire&nbsp;<code>timeToLive<\/code>&nbsp;seconds after the (potentially different)&nbsp;<code>currentTime<\/code>.<\/p>\n\n\n\n<p>Implement the&nbsp;<code>AuthenticationManager<\/code>&nbsp;class:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>AuthenticationManager(int timeToLive)<\/code>&nbsp;constructs the&nbsp;<code>AuthenticationManager<\/code>&nbsp;and sets the&nbsp;<code>timeToLive<\/code>.<\/li><li><code>generate(string tokenId, int currentTime)<\/code>&nbsp;generates a new token with the given&nbsp;<code>tokenId<\/code>&nbsp;at the given&nbsp;<code>currentTime<\/code>&nbsp;in seconds.<\/li><li><code>renew(string tokenId, int currentTime)<\/code>&nbsp;renews the&nbsp;<strong>unexpired<\/strong>&nbsp;token with the given&nbsp;<code>tokenId<\/code>&nbsp;at the given&nbsp;<code>currentTime<\/code>&nbsp;in seconds. If there are no unexpired tokens with the given&nbsp;<code>tokenId<\/code>, the request is ignored, and nothing happens.<\/li><li><code>countUnexpiredTokens(int currentTime)<\/code>&nbsp;returns the number of&nbsp;<strong>unexpired<\/strong>&nbsp;tokens at the given currentTime.<\/li><\/ul>\n\n\n\n<p>Note that if a token expires at time&nbsp;<code>t<\/code>, and another action happens on time&nbsp;<code>t<\/code>&nbsp;(<code>renew<\/code>&nbsp;or&nbsp;<code>countUnexpiredTokens<\/code>), the expiration takes place&nbsp;<strong>before<\/strong>&nbsp;the other actions.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><img decoding=\"async\" alt=\"\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/02\/25\/copy-of-pc68_q2.png\"><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input<\/strong>\n[\"AuthenticationManager\", \"<code>renew<\/code>\", \"generate\", \"<code>countUnexpiredTokens<\/code>\", \"generate\", \"<code>renew<\/code>\", \"<code>renew<\/code>\", \"<code>countUnexpiredTokens<\/code>\"]\n[[5], [\"aaa\", 1], [\"aaa\", 2], [6], [\"bbb\", 7], [\"aaa\", 8], [\"bbb\", 10], [15]]\n<strong>Output<\/strong>\n[null, null, null, 1, null, null, null, 0]\n<\/pre>\n\n\n\n<p><strong>Explanation<\/strong> AuthenticationManager authenticationManager = new AuthenticationManager(5); \/\/ Constructs the AuthenticationManager with <code>timeToLive<\/code> = 5 seconds. authenticationManager.<code>renew<\/code>(&#8220;aaa&#8221;, 1); \/\/ No token exists with tokenId &#8220;aaa&#8221; at time 1, so nothing happens. authenticationManager.generate(&#8220;aaa&#8221;, 2); \/\/ Generates a new token with tokenId &#8220;aaa&#8221; at time 2. authenticationManager.<code>countUnexpiredTokens<\/code>(6); \/\/ The token with tokenId &#8220;aaa&#8221; is the only unexpired one at time 6, so return 1. authenticationManager.generate(&#8220;bbb&#8221;, 7); \/\/ Generates a new token with tokenId &#8220;bbb&#8221; at time 7. authenticationManager.<code>renew<\/code>(&#8220;aaa&#8221;, 8); \/\/ The token with tokenId &#8220;aaa&#8221; expired at time 7, and 8 &gt;= 7, so at time 8 the <code>renew<\/code> request is ignored, and nothing happens. authenticationManager.<code>renew<\/code>(&#8220;bbb&#8221;, 10); \/\/ The token with tokenId &#8220;bbb&#8221; is unexpired at time 10, so the <code>renew<\/code> request is fulfilled and now the token will expire at time 15. authenticationManager.<code>countUnexpiredTokens<\/code>(15); \/\/ The token with tokenId &#8220;bbb&#8221; expires at time 15, and the token with tokenId &#8220;aaa&#8221; expired at time 7, so currently no token is unexpired, so return 0.<\/p>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= timeToLive &lt;= 10<sup>8<\/sup><\/code><\/li><li><code>1 &lt;= currentTime &lt;= 10<sup>8<\/sup><\/code><\/li><li><code>1 &lt;= tokenId.length &lt;= 5<\/code><\/li><li><code>tokenId<\/code>&nbsp;consists only of lowercase letters.<\/li><li>All calls to&nbsp;<code>generate<\/code>&nbsp;will contain unique values of&nbsp;<code>tokenId<\/code>.<\/li><li>The values of&nbsp;<code>currentTime<\/code>&nbsp;across all the function calls will be&nbsp;<strong>strictly increasing<\/strong>.<\/li><li>At most&nbsp;<code>2000<\/code>&nbsp;calls will be made to all functions combined.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Use a hashtable to store the token and its expiration time.<\/p>\n\n\n\n<p>Time complexity: at most O(n) per operation<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, 160 ms, 30.1 MB\nclass AuthenticationManager {\npublic:\n  AuthenticationManager(int timeToLive) : ttl_(timeToLive) {}\n\n  void generate(string tokenId, int currentTime) {\n    clear(currentTime);\n    tokens_[tokenId] = currentTime + ttl_;\n  }\n\n  void renew(string tokenId, int currentTime) {\n    clear(currentTime);\n    if (!tokens_.count(tokenId)) return;    \n    tokens_[tokenId] = currentTime + ttl_;\n  }\n\n  int countUnexpiredTokens(int currentTime) {\n    clear(currentTime);\n    return tokens_.size();\n  }\nprivate:\n  void clear(int currentTime) {\n    vector<string> ids;\n    for (const auto& [id, t] : tokens_)\n      if (t <= currentTime) ids.push_back(id);\n    for (const string&#038; id : ids)\n      tokens_.erase(id);\n  }\n  \n  unordered_map<string, int> tokens_;\n  int ttl_;\n};\n\n\/**\n * Your AuthenticationManager object will be instantiated and called as such:\n * AuthenticationManager* obj = new AuthenticationManager(timeToLive);\n * obj->generate(tokenId,currentTime);\n * obj->renew(tokenId,currentTime);\n * int param_3 = obj->countUnexpiredTokens(currentTime);\n *\/\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is an authentication system that works with authentication tokens. For each session, the user will receive a new authentication token that will expire&nbsp;timeToLive&nbsp;seconds after&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[251,82,177],"class_list":["post-8252","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-design","tag-hashtable","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8252"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8252\/revisions"}],"predecessor-version":[{"id":8254,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8252\/revisions\/8254"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}