Formatted question description: https://leetcode.ca/all/552.html

# 552. Student Attendance Record II (Hard)

Given a positive integer n, 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 109 + 7.

A student attendance record is a string that only contains the following three characters:

1. 'A' : Absent.
2. 'L' : Late.
3. 'P' : Present.

A record is regarded as rewardable if it doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late).

Example 1:

Input: n = 2
Output: 8
Explanation:
There are 8 records with length 2 will be regarded as rewardable:
"PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL"
Only "AA" won't be regarded as rewardable owing to more than one absent times.


Note: The value of n won't exceed 100,000.

Related Topics:
Dynamic Programming

Similar Questions:

## Solution 1. Simulation

At each step, we have 3 options, append ‘A’, ‘L’, or ‘P’.

Let v[i][A][L] is the number of all possible records that have i + 1 charactors and only have A ‘A’s and consecutive L ‘L’s.

Assume we are visiting v[i][A][L] and v[i][A][L] = val.

If we append ‘A’, then the next state will be [i + 1][A + 1], so v[i + 1][A + 1] += val. Note that we can’t do this if A == 1.

If we append ‘L’, then the next state will be [i + 1][A][L + 1], so v[i + 1][A][L + 1] += val. Note that we can’t do this if L == 2.

If we append ‘P’, then the next state will be [i + 1][A], so v[i + 1][A] += val.

// OJ: https://leetcode.com/problems/student-attendance-record-ii/

// Time: O(N)
// Space: O(N)
class Solution {
public:
int checkRecord(int n) {
auto v = vector<vector<vector<int>>>(n + 1, vector<vector<int>>(2, vector<int>(3)));
long long ans = 0, mod = 1e9 + 7;
for (int i = 0; i < n; ++i) {
for (int A = 0; A <= 1; ++A) {
for (int L = 0; L <= 2; ++L) {
if (A + L > i + 1) continue;
int val = i == 0 ? 1 : v[i][A][L];
if (A != 1) v[i + 1][A + 1] = (v[i + 1][A + 1] + val) % mod;
if (L != 2) v[i + 1][A][L + 1] = (v[i + 1][A][L + 1] + val) % mod;
v[i + 1][A] = (v[i + 1][A] + val) % mod;
if (i == n - 1) ans = (ans + val) % mod;
}
}
}
return ans;
}
};


## Solution 2. Simulation + Space Optimization

Since only two layers are related (i and i + 1), we can reduce the array from N * 2 * 3 to 2 * 2 * 3.

// OJ: https://leetcode.com/problems/student-attendance-record-ii/

// Time: O(N)
// Space: O(1)
class Solution {
public:
int checkRecord(int n) {
auto v = vector<vector<vector<int>>>(2, vector<vector<int>>(2, vector<int>(3)));
long long ans = 0, mod = 1e9 + 7;
for (int i = 0; i < n; ++i) {
for (int A = 0; A <= 1; ++A) {
for (int L = 0; L <= 2; ++L) {
v[(i + 1) % 2][A][L] = 0;
}
}
for (int A = 0; A <= 1; ++A) {
for (int L = 0; L <= 2; ++L) {
if (A + L > i + 1) continue;
int val = i == 0 ? 1 : v[i % 2][A][L];
if (A != 1) v[(i + 1) % 2][A + 1] = (v[(i + 1) % 2][A + 1] + val) % mod;
if (L != 2) v[(i + 1) % 2][A][L + 1] = (v[(i + 1) % 2][A][L + 1] + val) % mod;
v[(i + 1) % 2][A] = (v[(i + 1) % 2][A] + val) % mod;
if (i == n - 1) ans = (ans + val) % mod;
}
}
}
return ans;
}
};


Java

class Solution {
public int checkRecord(int n) {
final int MODULO = 1000000007;
long[][] dp = new long[n];
dp = 1;
dp = 1;
dp = 1;
for (int i = 1; i < n; i++) {
dp[i] = (dp[i - 1] + dp[i - 1] + dp[i - 1]) % MODULO;
dp[i] = dp[i - 1];
dp[i] = (dp[i - 1] + dp[i - 1]) % MODULO;
dp[i] = dp[i - 1];
dp[i] = dp[i - 1];
dp[i] = (dp[i - 1] + dp[i - 1] + dp[i - 1]) % MODULO;
dp[i] = (dp[i - 1] + dp[i - 1] + dp[i - 1] + dp[i - 1]) % MODULO;
}
long sum = 0;
for (int i = 0; i < 7; i++)
sum = (sum + dp[n - 1][i]) % MODULO;
int res = (int) sum;
return res;
}
}