难度 困难
题目 Leetcode:
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:
'A': Absent.
'L': Late.
'P': Present.
Any student is eligible for an attendance award if they meet both of the following criteria:
The student was absent ('A') for strictly fewer than 2 days total.
The student was never late ('L') for 3 or more consecutive days.
Given an integer n, return the number of possible attendance records of length n that make a student eligible for an attendance award. The answer may be very large, so return it modulo 109 + 7.
题目解析
这道题用到的动态规划,虽然是想到了,但是还是卡了很久,最后还是看到大佬的一张图才悟了,还是太弱了,简单来说就是只有6种情况,分别记录遍历就好了。
具体的还是看代码吧,因为这个懂了就贼简单。
1 class Solution { 2 public: 3 int checkRecord(int n) { 4 long long a[6]={1,1,0,1,0,0}; 5 long long b[6]; 6 int mod=1e9+7; 7 for(int i=2;i<=n;i++) 8 { 9 b[0]=(a[0]+a[1]+a[2])%mod; 10 b[1]=a[0]; 11 b[2]=a[1]; 12 b[3]=(a[0]+a[1]+a[2]+a[3]+a[4]+a[5])%mod; 13 b[4]=a[3]; 14 b[5]=a[4]; 15 for(int i=0;i<6;i++)a[i]=b[i]; 16 } 17 long long ans=0; 18 for(int i=0;i<6;i++)ans=(ans+a[i])%mod; 19 return ans; 20 } 21 };