552 Student Attendance Record II 学生出勤记录 II
给定一个正整数 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的更多相关文章
- [LeetCode] Student Attendance Record I 学生出勤记录之一
You are given a string representing an attendance record for a student. The record only contains the ...
- Leetcode551.Student Attendance Record I学生出勤记录1
给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : Present,到场 如果一个学生的出勤纪录中不超过一个' ...
- LeetCode 551. Student Attendance Record I (学生出勤纪录 I)
You are given a string representing an attendance record for a student. The record only contains the ...
- 551 Student Attendance Record I 学生出勤纪录 I
给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : Present,到场如果一个学生的出勤纪 ...
- Java实现 LeetCode 552 学生出勤记录 II(数学转换?还是动态规划?)
552. 学生出勤记录 II 给定一个正整数 n,返回长度为 n 的所有可被视为可奖励的出勤记录的数量. 答案可能非常大,你只需返回结果mod 109 + 7的值. 学生出勤记录是只包含以下三个字符的 ...
- Leetcode 552.学生出勤记录II
学生出勤记录II 给定一个正整数 n,返回长度为 n 的所有可被视为可奖励的出勤记录的数量. 答案可能非常大,你只需返回结果mod 109 + 7的值. 学生出勤记录是只包含以下三个字符的字符串: ' ...
- [LeetCode] 552. Student Attendance Record II 学生出勤记录之二
Given a positive integer n, return the number of all possible attendance records with length n, whic ...
- [Swift]LeetCode552. 学生出勤记录 II | Student Attendance Record II
Given a positive integer n, return the number of all possible attendance records with length n, whic ...
- [LeetCode] Student Attendance Record II 学生出勤记录之二
Given a positive integer n, return the number of all possible attendance records with length n, whic ...
随机推荐
- 侧方位停车想一次过,掌握边线30cm很重要!
侧方位停车要想一次过关,关键在于保持车身距离库边线30cm左右的距离.但是,往往有很多学员掌控不好这个距离,导致倒库时压线.那么,如何找准这个30cm呢?下面,小编就来教大家方法,赶紧学习吧! 侧方位 ...
- python 解析配置文件
settings.cfg [english] greeting = Hello [french] greeting = Bonjour [files] home = /usr/local bin = ...
- 编译Thrift
按照 https://syslint.com/blog/tutorial/how-to-install-apache-thrift-on-ubuntu-14-04/ 进行, 编译时出现错误 make[ ...
- 并不对劲的bzoj1095:p2056:[ZJOI2007]捉迷藏
题目大意 给一\(n\)(\(n\leq10^5\))个点的一棵树,每个点有可能是黑色或白色,一开始所有点都是黑色,支持以下两种操作: 1.改变一个点的颜色 2.询问最远的黑色点对的距离 题解 据说是 ...
- vmware 克隆linux虚拟机后的网卡修改方法
VMware虚拟机安装好CentOS6.5系统后,纯净的系统多克隆几份出来方便后期做试验.克隆步骤很简单,克隆后出现的问题是克隆后的网卡MAC地址和原系统MAC地址一样,在局域网内会有冲突,需要更改克 ...
- Java笔记(四)
13. 集合框架: 集合中存储的都是对象的引用(地址) 迭代器:集合的取出元素的方式 import java.util.ArrayList; import java.util.Iterator; pu ...
- xen添加网卡
brctl addbr xenbr0 ifconfig xenbr0 up ifconfig xenbr0 192.168.0.1 /etc/xen/scripts/network-bridge st ...
- saltstack master minion安装配置简单使用
首先先了解下saltstack是什么,为什么使用它 它与Ansible.Puppet都属于集中管理工具,由于现在企业规模的不断庞大及业务的增长,所需要管理的服务器数量增多,而且大部分机器都属于同类业务 ...
- Ubuntu install JDK
1.#下载JDK,记住保存的目录 2. sudo mkdir /usr/java 3. sudo tar zxvf jdk-7u75-linux-x64.tar.gz -C /usr/java 4. ...
- wp8环境搭建
Windows Phone 8将采用与Windows 8相同的NT内核,这就意味着WP8将可能兼容Win8应用,开发者仅需很少改动就能让应用在两个平台上运行.由于内核变更,Windows Phone ...