Leet Code Daily 2024-05-26
Leet Code Daily 2024-05-26 - Student Attendance Record II
Solution
It's just math..
Let's say is the number of possible student attendance records of which:
- The last record is
P. - It contains no record
A. - It has no more than two consecutive
Lrecords.
Let's say is the number of possible student attendance records of which:
- It contains no record
A. - It has no more than two consecutive
Lrecords.
Now considering at most one record A, the final answer would be:
Code
constexpr long long kMod = 1E9 + 7;
class Solution {
public:
int checkRecord(int n) {
vector<long long> f = {1, 1, 2, 4}, g;
for (int i = 4; i <= n; i++) {
f.push_back((f[i - 1] + f[i - 2] + f[i - 3]) % kMod);
}
for (int i = 0; i <= n; i++) {
g.push_back(accumulate(next(f.begin(), max(i - 2, 0)),
next(f.begin(), i + 1), 0LL) %
kMod);
}
long long ans = g[n];
for (int i = 0; i <= n - 1; i++) {
ans = (ans + g[i] * g[n - i - 1]) % kMod;
}
return ans;
}
};