给定一个正整数 n,返回长度为 n 的所有可被视为可奖励的出勤记录的数量。 答案可能非常大,你只需返回结果mod 109 + 7的值。
学生出勤记录是只包含以下三个字符的字符串:
    1.'A' : Absent,缺勤
    2.'L' : Late,迟到
    3.'P' : Present,到场
如果记录不包含多于一个'A'(缺勤)或超过两个连续的'L'(迟到),则该记录被视为可奖励的。
示例:
输入: n = 2
输出: 8
解释:
有8个长度为2的记录将被视为可奖励:
"PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL"
只有"AA"不会被视为可奖励,因为缺勤次数超过一次。
注意:n 的值不会超过100000。
详见:https://leetcode.com/problems/student-attendance-record-ii/description/

C++:

class Solution {
public:
int checkRecord(int n)
{
int M = 1000000007;
int dp[n + 1][2][3] = {0};
for (int j = 0; j < 2; ++j)
{
for (int k = 0; k < 3; ++k)
{
dp[0][j][k] = 1;
}
}
for (int i = 1; i <= n; ++i)
{
for (int j = 0; j < 2; ++j)
{
for (int k = 0; k < 3; ++k)
{
int val = dp[i - 1][j][2];
if (j > 0)
{
val = (val + dp[i - 1][j - 1][2]) % M;
}
if (k > 0)
{
val = (val + dp[i - 1][j][k - 1]) % M;
}
dp[i][j][k] = val;
}
}
}
return dp[n][1][2];
}
};

参考:http://www.cnblogs.com/grandyang/p/6866756.html

552 Student Attendance Record II 学生出勤记录 II的更多相关文章

  1. [LeetCode] Student Attendance Record I 学生出勤记录之一

    You are given a string representing an attendance record for a student. The record only contains the ...

  2. Leetcode551.Student Attendance Record I学生出勤记录1

    给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : Present,到场 如果一个学生的出勤纪录中不超过一个' ...

  3. LeetCode 551. Student Attendance Record I (学生出勤纪录 I)

    You are given a string representing an attendance record for a student. The record only contains the ...

  4. 551 Student Attendance Record I 学生出勤纪录 I

    给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符:    'A' : Absent,缺勤    'L' : Late,迟到    'P' : Present,到场如果一个学生的出勤纪 ...

  5. Java实现 LeetCode 552 学生出勤记录 II(数学转换?还是动态规划?)

    552. 学生出勤记录 II 给定一个正整数 n,返回长度为 n 的所有可被视为可奖励的出勤记录的数量. 答案可能非常大,你只需返回结果mod 109 + 7的值. 学生出勤记录是只包含以下三个字符的 ...

  6. Leetcode 552.学生出勤记录II

    学生出勤记录II 给定一个正整数 n,返回长度为 n 的所有可被视为可奖励的出勤记录的数量. 答案可能非常大,你只需返回结果mod 109 + 7的值. 学生出勤记录是只包含以下三个字符的字符串: ' ...

  7. [LeetCode] 552. Student Attendance Record II 学生出勤记录之二

    Given a positive integer n, return the number of all possible attendance records with length n, whic ...

  8. [Swift]LeetCode552. 学生出勤记录 II | Student Attendance Record II

    Given a positive integer n, return the number of all possible attendance records with length n, whic ...

  9. [LeetCode] Student Attendance Record II 学生出勤记录之二

    Given a positive integer n, return the number of all possible attendance records with length n, whic ...

随机推荐

  1. top命令按内存和cpu排序

    目录 按进程的CPU使用率排序 按进程的内存使用率排序 按进程的CPU使用率排序 运行top命令后,键入大写P. 有两种途径: a) 打开大写键盘的情况下,直接按P键 b) 未打开大写键盘的情况下,S ...

  2. Android应用资源---动画资源(Animation Resources)

    有两种类型的动画资源: 属性动画 在设定的时间内,通过修改与Animator类相关的对象的属性值来创建一个动画. 视图动画 有两种类型的视图动画框架 补间动画(Tween animation):通过执 ...

  3. BestCoder10 1001 Revenge of Fibonacci(hdu 5018) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5018 题目意思:给出在 new Fibonacci 中最先的两个数 A 和 B(也就是f[1] = A ...

  4. tflearn 中文汉字识别,训练后模型存为pb给TensorFlow使用——模型层次太深,或者太复杂训练时候都不会收敛

    tflearn 中文汉字识别,训练后模型存为pb给TensorFlow使用. 数据目录在data,data下放了汉字识别图片: data$ ls0  1  10  11  12  13  14  15 ...

  5. poj2226Muddy Fields——二分图匹配

    题目:http://poj.org/problem?id=2226 把行连通块作为左部点,列连通块作为右部点,行列连通块有相交的格子就连边: 则问题转化为求最小点覆盖,即最大匹配. 代码如下: #in ...

  6. STL中关于vector的一点有趣的事情

    PLZ ADD SOURCE: http://www.cnblogs.com/xdxer/p/4072056.html 今日饭后,一哥发给我一段代码,让我看看会不会有什么问题. #include< ...

  7. docker容器firewalld端口转发规则

    docker容器firewalld端口转发规则 1.配置firewalld端口转发,要先打开端口转发,则需要先 #firewall-cmd --zone=public --add-maspuerade ...

  8. windows下mysql5.1忘记root密码解决方法[win7]

    步骤如下:1.停止mysql服务(以管理员身份,在cmd命令行下运行) net stop mysql2.使用 mysqld –skip-grant-tables 命令启动mysql数据库 D:\> ...

  9. eclipse编译Jmeter源码

    1.在apache官网下载源码和安装包   http://jmeter.apache.org/ 2.  解压     解压安装包和源码包,     将安装包apache-jmeter-3.3 里lib ...

  10. 【Data Structure & Algorithm】求1+2+…+n

    求1+2+-+n 题目:求1+2+-+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A ? B : C). 分析:此题没多少实际意义,因为 ...