{"id":2286,"date":"2018-03-22T00:08:36","date_gmt":"2018-03-22T07:08:36","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2286"},"modified":"2018-03-22T00:12:54","modified_gmt":"2018-03-22T07:12:54","slug":"leetcode-552-student-attendance-record-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-552-student-attendance-record-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 552. Student Attendance Record II"},"content":{"rendered":"<div class=\"question-description\">\n<div>\n<h1><strong>Problem<\/strong><\/h1>\n<p>Given a positive integer\u00a0<b>n<\/b>, return the number of all possible attendance records with length n, which will be regarded as rewardable. The answer may be very large, return it after mod 10<sup>9<\/sup>\u00a0+ 7.<\/p>\n<p>A student attendance record is a string that only contains the following three characters:<\/p>\n<ol>\n<li><b>&#8216;A&#8217;<\/b>\u00a0: Absent.<\/li>\n<li><b>&#8216;L&#8217;<\/b>\u00a0: Late.<\/li>\n<li><b>&#8216;P&#8217;<\/b>\u00a0: Present.<\/li>\n<\/ol>\n<p>A record is regarded as rewardable if it doesn&#8217;t contain\u00a0<b>more than one &#8216;A&#8217; (absent)<\/b>\u00a0or\u00a0<b>more than two continuous &#8216;L&#8217; (late)<\/b>.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> n = 2\r\n<b>Output:<\/b> 8 \r\n<b>Explanation:<\/b>\r\nThere are 8 records with length 2 will be regarded as rewardable:\r\n\"PP\" , \"AP\", \"PA\", \"LP\", \"PL\", \"AL\", \"LA\", \"LL\"\r\nOnly \"AA\" won't be regarded as rewardable owing to more than one absent times. \r\n<\/pre>\n<p><b>Note:<\/b>\u00a0The value of\u00a0<b>n<\/b>\u00a0won&#8217;t exceed 100,000.<\/p>\n<\/div>\n<\/div>\n<h1><strong>Solution<\/strong><\/h1>\n<p>C++<\/p>\n<p>DFS w\/ memorization (stack overflow)<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\r\npublic:\r\n  int checkRecord(int n) {\r\n    m_ = vector&lt;vector&lt;int&gt;&gt;(n + 1, vector&lt;int&gt;(6, 0));\r\n    if (n &gt;= 100000) return 0;\r\n    return dfs(n, 1, 2);\r\n  }\r\nprivate:\r\n  vector&lt;vector&lt;int&gt;&gt; m_;\r\n  \/\/ number of solutions of using at most A As and L Ls\r\n  long dfs(int n, int A, int L) {    \r\n    if (n == 0) return 1;\r\n    int key = A * 3 + L;\r\n    if (m_[n][key]) return m_[n][key];\r\n    long ans = 0;\r\n    ans += dfs(n - 1, A, 2); \/\/ 'P', reset number of Ls\r\n    if (A &gt; 0)\r\n      ans += dfs(n - 1, A - 1, 2); \/\/ 'A', reset number of Ls\r\n    if (L &gt; 0)\r\n      ans += dfs(n - 1, A, L - 1); \/\/ 'L'    \r\n    return m_[n][key] = (ans % 1000000007);\r\n  }\r\n};<\/pre>\n<p>DP<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 129 ms\r\nclass Solution {\r\npublic:\r\n  int checkRecord(int n) {\r\n    constexpr int kMod = 1000000007;\r\n    vector&lt;long&gt; dp(6, 1);\r\n    for (int i = 1; i &lt;= n; ++i) {\r\n      vector&lt;long&gt; tmp(6);\r\n      for (int A = 0; A &lt;= 1; ++A)\r\n        for (int L = 0; L &lt;= 2; ++L) {\r\n          const int key = getKey(A, L);\r\n          tmp[key] += dp[getKey(A, 2)];\r\n          if (A &gt; 0) tmp[key] += dp[getKey(A - 1, 2)];\r\n          if (L &gt; 0) tmp[key] += dp[getKey(A, L - 1)];   \r\n          tmp[key] %= kMod;\r\n        }\r\n      dp.swap(tmp);\r\n    }\r\n    return dp[5];\r\n  }\r\nprivate:\r\n  inline int getKey(int A, int L) { return A * 3 + L; }\r\n};<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a positive integer\u00a0n, return the number of all possible attendance records with length n, which will be regarded as rewardable. The answer may&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[8,18],"class_list":["post-2286","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-counting","tag-dp","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2286","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=2286"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2286\/revisions"}],"predecessor-version":[{"id":2290,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2286\/revisions\/2290"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}