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][0], so v[i + 1][A + 1][0] += 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][0], so v[i + 1][A][0] += 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][0] = (v[i + 1][A + 1][0] + val) % mod;
if (L != 2) v[i + 1][A][L + 1] = (v[i + 1][A][L + 1] + val) % mod;
v[i + 1][A][0] = (v[i + 1][A][0] + 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][0] = (v[(i + 1) % 2][A + 1][0] + 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][0] = (v[(i + 1) % 2][A][0] + val) % mod;
if (i == n - 1) ans = (ans + val) % mod;
}
}
}
return ans;
}
};

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

############

class Solution {
private static final int MOD = 1000000007;

public int checkRecord(int n) {
long[][][] dp = new long[n][2][3];

// base case
dp[0][0][0] = 1;
dp[0][0][1] = 1;
dp[0][1][0] = 1;

for (int i = 1; i < n; i++) {
// A
dp[i][1][0] = (dp[i - 1][0][0] + dp[i - 1][0][1] + dp[i - 1][0][2]) % MOD;
// L
dp[i][0][1] = dp[i - 1][0][0];
dp[i][0][2] = dp[i - 1][0][1];
dp[i][1][1] = dp[i - 1][1][0];
dp[i][1][2] = dp[i - 1][1][1];
// P
dp[i][0][0] = (dp[i - 1][0][0] + dp[i - 1][0][1] + dp[i - 1][0][2]) % MOD;
dp[i][1][0] = (dp[i][1][0] + dp[i - 1][1][0] + dp[i - 1][1][1] + dp[i - 1][1][2]) % MOD;
}

long ans = 0;
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) {
ans = (ans + dp[n - 1][j][k]) % MOD;
}
}
return (int) ans;
}
}


• // 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][0] = (v[i + 1][A + 1][0] + val) % mod;
if (L != 2) v[i + 1][A][L + 1] = (v[i + 1][A][L + 1] + val) % mod;
v[i + 1][A][0] = (v[i + 1][A][0] + val) % mod;
if (i == n - 1) ans = (ans + val) % mod;
}
}
}
return ans;
}
};

• class Solution:
def checkRecord(self, n: int) -> int:
mod = int(1e9 + 7)
dp = [[[0, 0, 0], [0, 0, 0]] for _ in range(n)]

# base case
dp[0][0][0] = dp[0][0][1] = dp[0][1][0] = 1

for i in range(1, n):
# A
dp[i][1][0] = (dp[i - 1][0][0] + dp[i - 1][0][1] + dp[i - 1][0][2]) % mod
# L
dp[i][0][1] = dp[i - 1][0][0]
dp[i][0][2] = dp[i - 1][0][1]
dp[i][1][1] = dp[i - 1][1][0]
dp[i][1][2] = dp[i - 1][1][1]
# P
dp[i][0][0] = (dp[i - 1][0][0] + dp[i - 1][0][1] + dp[i - 1][0][2]) % mod
dp[i][1][0] = (
dp[i][1][0] + dp[i - 1][1][0] + dp[i - 1][1][1] + dp[i - 1][1][2]
) % mod

ans = 0
for j in range(2):
for k in range(3):
ans = (ans + dp[n - 1][j][k]) % mod
return ans

############

class Solution(object):
def checkRecord(self, n):
"""
:type n: int
:rtype: int
"""
M = 10 ** 9 + 7
dp = [0] * (n + 1)
dp[:3] = [1, 2, 4]

for i in range(3, n + 1):
dp[i] = (dp[i - 1] + dp[i - 2] + dp[i - 3]) % M
ans = dp[n]

for i in range(1, n + 1):
ans += (dp[i - 1] * dp[n - i]) % M
ans %= M
return ans % M


• const _mod int = 1e9 + 7

func checkRecord(n int) int {
dp := make([][][]int, n)
for i := 0; i < n; i++ {
dp[i] = make([][]int, 2)
for j := 0; j < 2; j++ {
dp[i][j] = make([]int, 3)
}
}

// base case
dp[0][0][0] = 1
dp[0][0][1] = 1
dp[0][1][0] = 1

for i := 1; i < n; i++ {
// A
dp[i][1][0] = (dp[i-1][0][0] + dp[i-1][0][1] + dp[i-1][0][2]) % _mod
// L
dp[i][0][1] = dp[i-1][0][0]
dp[i][0][2] = dp[i-1][0][1]
dp[i][1][1] = dp[i-1][1][0]
dp[i][1][2] = dp[i-1][1][1]
// P
dp[i][0][0] = (dp[i-1][0][0] + dp[i-1][0][1] + dp[i-1][0][2]) % _mod
dp[i][1][0] = (dp[i][1][0] + dp[i-1][1][0] + dp[i-1][1][1] + dp[i-1][1][2]) % _mod
}

var ans int
for j := 0; j < 2; j++ {
for k := 0; k < 3; k++ {
ans = (ans + dp[n-1][j][k]) % _mod
}
}
return ans
}