给定一个正整数 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. C++的逐过程和逐语句的区别

    1.逐语句是指在遇到函数调用语句的时候进入到函数内部执行. 2.逐过程是指在遇到函数调用语句时把函数当作一条语句执行.

  2. cassandra 存储二进制data

    Blob type The Cassandra blob data type represents a constant hexadecimal number defined as 0[xX](hex ...

  3. Grunt 自动编译 Less 文件配置

    1.安装Grunt http://www.gruntjs.net/getting-started 2.编辑 package.json 文件 { "name": "Grun ...

  4. Mac开发快速入门

    初次接触mac开发,发现国内相关资料少得可怜,于是写下这篇文章,作为学习记录.Mac应用开发也是使用Objective-C进行开发的,所以从iOS转Mac并不困难,很多东西都一样. 本文以一个登录界面 ...

  5. 关于Spring MVC分页

    使用Pageable接口,首先要实例化. 在servlet-context.xml中配置 <annotation-driven> <!-- 分页参数 --> <argum ...

  6. MFC中显示一张位图

    1.用类CBitmap加载位图 2.创建内存DC, 将位图选进此内存DC 3.调用BitBlt将内存DC的内容拷贝到其它DC(通知是显示DC) 例子(来自MSDN): // This OnDraw() ...

  7. poi word 转html (.DOC .DOCX )

    注:不支持图片,支持表格 package com.bjhy.platform.report.commons; import java.io.BufferedWriter; import java.io ...

  8. 经验收获Linux终端下方便命令

    一.命令行日常系快捷键 如下的快捷方式非常有用,能够极大的提升你的工作效率: CTRL + U - 剪切光标前的内容 CTRL + K - 剪切光标至行末的内容 CTRL + Y - 粘贴 CTRL ...

  9. css样式 -- 表格不会因为字体过长导致字体溢出的问题

    常常碰到因为表格大小就麽大了,字体过长会爆炸溢出的问题,我们后端就用这个可以了,溢出的可以省略号 ... 代替好了. /* 在表格css样式加上这三个就可以了 效果就会变成 “abc...” */ { ...

  10. 爬虫库之BeautifulSoup学习(一)

    Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据. 官方解释如下: Beautiful Soup提供一些简单的.pytho ...