leetcode 552. Student Attendance Record II

552. Student Attendance Record II

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.

class Solution {
public:
    int checkRecord(int n) 
    {
        int m = 1e9+7;
        vector<int> P(n,0);
        vector<int> L(n,0);
        vector<int> A(n,0);
        if(n==1) return 3;
        if(n==2) return 8;
        P[0] = 1; P[1] = 3;
        L[0] = 1; L[1] = 3;
        A[0] = 1; A[1] = 2; A[2] = 4;
        for(int i=2;i<n;i++)
        {
            P[i] = ((A[i-1]%m + P[i-1]%m)%m + L[i-1]%m)%m;
            L[i] = (((A[i-1]%m + A[i-2]%m)%m + P[i-1]%m)%m + P[i-2]%m)%m;
            if(i>2) A[i] = ((A[i-1]%m + A[i-2]%m)%m + A[i-3]%m)%m;
        }
        return ((A[n-1]%m + P[n-1]%m)%m + L[n-1]%m)%m;  
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值