题目:
给定一个正整数 n,返回长度为 n 的所有可被视为可奖励的出勤记录的数量。 答案可能非常大,你只需返回结果mod 109 + 7的值。
学生出勤记录是只包含以下三个字符的字符串:
'A' : Absent,缺勤
'L' : Late,迟到
'P' : Present,到场
如果记录不包含多于一个'A'(缺勤)或超过两个连续的'L'(迟到),则该记录被视为可奖励的。
示例 1:
输入: n = 2
输出: 8
解释:
有8个长度为2的记录将被视为可奖励:
"PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL"
只有"AA"不会被视为可奖励,因为缺勤次数超过一次。
注意:n 的值不会超过100000。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/student-attendance-record-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
class Solution {
public:
int checkRecord(int n) {
if (n == 1) return 3;
int mod = 1000000007;
int* A = new int[n + 1];
int* L = new int[n + 1];
int* P = new int[n + 1];
int* noAL = new int[n + 1];
int* noAP = new int[n + 1];
//int A[10], L[10], P[10], noAL[10], noAP[10];
A[1] = L[1] = P[1] = 1;
L[2] = 3;
noAL[1] = noAP[1] = 1;
noAL[2] = noAP[2] = 2;
for (int i = 2; i <= n; i++) {
P[i] = ((P[i - 1] + A[i - 1])%mod + L[i - 1])%mod;
A[i] = (noAP[i - 1] + noAL[i - 1]) % mod;
noAP[i] = (noAP[i - 1] + noAL[i - 1]) % mod;
if (i >= 3) {
L[i] = ((P[i - 1] + A[i - 1]) % mod + (A[i - 2] + P[i - 2]) % mod) % mod;
noAL[i] = (noAP[i - 1] + noAP[i - 2]) % mod;
}
}
return ((A[n] + P[n]) % mod + L[n]) % mod;
}
};
思路:分情况讨论,然后对每一类进行动态规划。