<p>An attendance record for a student can be represented as a string where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:</p>

<ul>
	<li><code>&#39;A&#39;</code>: Absent.</li>
	<li><code>&#39;L&#39;</code>: Late.</li>
	<li><code>&#39;P&#39;</code>: Present.</li>
</ul>

<p>Any student is eligible for an attendance award if they meet <strong>both</strong> of the following criteria:</p>

<ul>
	<li>The student was absent (<code>&#39;A&#39;</code>) for <strong>strictly</strong> fewer than 2 days <strong>total</strong>.</li>
	<li>The student was <strong>never</strong> late (<code>&#39;L&#39;</code>) for 3 or more <strong>consecutive</strong> days.</li>
</ul>

<p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of possible attendance records of length</em> <code>n</code><em> that make a student eligible for an attendance award. The answer may be very large, so return it <strong>modulo</strong> </em><code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are 8 records with length 2 that are eligible for an award:
&quot;PP&quot;, &quot;AP&quot;, &quot;PA&quot;, &quot;LP&quot;, &quot;PL&quot;, &quot;AL&quot;, &quot;LA&quot;, &quot;LL&quot;
Only &quot;AA&quot; is not eligible because there are 2 absences (there need to be fewer than 2).
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 3
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 10101
<strong>Output:</strong> 183236316
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution {
public:
    int f(int i, int absentCount, int lateCount, int mod, vector<vector<vector<int>>> &dp)
    {
        if(absentCount >= 2 || lateCount >=3)
            return 0;
        if(i == 0)
            return 1;
        if(dp[i][absentCount][lateCount] != -1)
            return dp[i][absentCount][lateCount];
        int p = f(i-1, absentCount, 0, mod, dp) % mod;
        int a = f(i-1, absentCount+1, 0, mod, dp) % mod;
        int l = f(i-1, absentCount, lateCount+1, mod, dp) % mod;
        return dp[i][absentCount][lateCount] = (((p + a) % mod) + l) % mod;
    }
    int checkRecord(int n) {
        int mod = (int)1e9 + 7;
        // vector<vector<vector<int>>> dp(n+1, vector<vector<int>>(2, vector<int> (3, -1)));
        // return f(n, 0, 0, mod, dp);

        /*
        vector<vector<vector<int>>> dp(n+1, vector<vector<int>>(3, vector<int> (4, 0)));
        for(int absentCount =0; absentCount<=1; absentCount++)
        {
            for(int lateCount =0; lateCount<=2; lateCount++)
            {
                dp[0][absentCount][lateCount] = 1;
            }
        }

        for(int i = 1; i <= n; i++)
        {
            for(int absentCount =0; absentCount<=1; absentCount++)
            {
                for(int lateCount =0; lateCount<=2; lateCount++)
                {
                    int p = dp[i-1][absentCount][0] % mod;
                    int a = dp[i-1][absentCount + 1][0] % mod;
                    int l = dp[i-1][absentCount][lateCount + 1] % mod;
                    dp[i][absentCount][lateCount] = (((p + a) % mod) + l) % mod;
                }
            }
        }

        return dp[n][0][0];
        */

        vector<vector<int>> prev(3, vector<int> (4, 0));
        for(int absentCount =0; absentCount<=1; absentCount++)
        {
            for(int lateCount =0; lateCount<=2; lateCount++)
            {
                prev[absentCount][lateCount] = 1;
            }
        }

        for(int i = 1; i <= n; i++)
        {
            vector<vector<int>> cur(3, vector<int> (4, 0));
            for(int absentCount =0; absentCount<=1; absentCount++)
            {
                for(int lateCount =0; lateCount<=2; lateCount++)
                {
                    int p = prev[absentCount][0] % mod;
                    int a = prev[absentCount + 1][0] % mod;
                    int l = prev[absentCount][lateCount + 1] % mod;
                    cur[absentCount][lateCount] = (((p + a) % mod) + l) % mod;
                }
            }
            prev = cur;
        }

        return prev[0][0];
    }
};
481734