Leetcode 552.学生出勤记录II
学生出勤记录II
给定一个正整数 n,返回长度为 n 的所有可被视为可奖励的出勤记录的数量。 答案可能非常大,你只需返回结果mod 109 + 7的值。
学生出勤记录是只包含以下三个字符的字符串:
- 'A' : Absent,缺勤
- 'L' : Late,迟到
- 'P' : Present,到场
如果记录不包含多于一个'A'(缺勤)或超过两个连续的'L'(迟到),则该记录被视为可奖励的。
示例 1:
输入: n = 2
输出: 8
解释:
有8个长度为2的记录将被视为可奖励:
"PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL"
只有"AA"不会被视为可奖励,因为缺勤次数超过一次。
注意:n 的值不会超过100000。
思路
首先,定义几个函数
Total(n): 字符长度为n的符合条件的个数
P(n): 字符长度为n的并且最后一位为P的符合条件的个数
L(n): 字符长度为n的并且最后一位为L的符合条件的个数
A(n):字符长度为n的并且最后一位为A的符合条件的个数
noAP(n):字符长度为n的并且没有一个A字符而且最后一位为P的符合条件的个数
noAL(n):字符长度为n的并且没有一个A字符而且最后一位为L的符合条件的个数
根据定义,可以得到
Total(n) = P(n) + L(n) + A(n); (n>=1)
P(n) = P(n-1) + L(n-1) + A(n-1); (n>=2)
然后由于规则规定不能有三个连续一起L字符,不然判定不符合条件,所以
L(n) = P(n-1) + A(n-1) + P(n-2) + A(n-2); (n>=3)
而且最多只能有一个A字符,因此
A(n) = noAP(n-1) + noAL(n-1); (n>=2)
同时,
noAP(n) = noAP(n-1) + noAL(n-1); (n>=2)
noAL(n) = noAP(n-1) + noAP(n-2); (n>=3)
由上面的关系式,然后我们再初始化一些条件,
P(1) = A(1) = L(1) = 1;
L(2) = 1;
noAL(1) = noAP(1) = 1;
noAL(2) = noAP(2) = 2;
class Solution {
public:
int checkRecord(int n) {
if (n == 1) return 3;
int mod = 1000000007;
int* A = new int[n + 1];
int* L = new int[n + 1];
int* P = new int[n + 1];
int* noAL = new int[n + 1];
int* noAP = new int[n + 1];
//int A[10], L[10], P[10], noAL[10], noAP[10];
A[1] = L[1] = P[1] = 1;
L[2] = 3;
noAL[1] = noAP[1] = 1;
noAL[2] = noAP[2] = 2;
for (int i = 2; i <= n; i++) {
P[i] = ((P[i - 1] + A[i - 1])%mod + L[i - 1])%mod;
A[i] = (noAP[i - 1] + noAL[i - 1]) % mod;
noAP[i] = (noAP[i - 1] + noAL[i - 1]) % mod;
if (i >= 3) {
L[i] = ((P[i - 1] + A[i - 1]) % mod + (A[i - 2] + P[i - 2]) % mod) % mod;
noAL[i] = (noAP[i - 1] + noAP[i - 2]) % mod;
}
}
return ((A[n] + P[n]) % mod + L[n]) % mod;
}
};
Leetcode 552.学生出勤记录II的更多相关文章
- Java实现 LeetCode 552 学生出勤记录 II(数学转换?还是动态规划?)
552. 学生出勤记录 II 给定一个正整数 n,返回长度为 n 的所有可被视为可奖励的出勤记录的数量. 答案可能非常大,你只需返回结果mod 109 + 7的值. 学生出勤记录是只包含以下三个字符的 ...
- 552 Student Attendance Record II 学生出勤记录 II
给定一个正整数 n,返回长度为 n 的所有可被视为可奖励的出勤记录的数量. 答案可能非常大,你只需返回结果mod 109 + 7的值.学生出勤记录是只包含以下三个字符的字符串: 1.'A' : ...
- [Swift]LeetCode552. 学生出勤记录 II | Student Attendance Record II
Given a positive integer n, return the number of all possible attendance records with length n, whic ...
- Java实现 LeetCode 551 学生出勤记录 I(暴力大法好)
551. 学生出勤记录 I 给定一个字符串来代表一个学生的出勤记录,这个记录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : Present,到场 如果一个 ...
- 力扣(LeetCode)学生出勤记录I 个人题解
给定一个字符串来代表一个学生的出勤记录,这个记录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : Present,到场 如果一个学生的出勤记录中不超过一个' ...
- 551.学生出勤记录I
/* * @lc app=leetcode.cn id=551 lang=java * * [551] 学生出勤记录 I * * https://leetcode-cn.com/problems/st ...
- hiho1482出勤记录II(string类字符串中查找字符串,库函数的应用)
string类中有很多好用的函数,这里介绍在string类字符串中查找字符串的函数. string类字符串中查找字符串一般可以用: 1.s.find(s1)函数,从前往后查找与目标字符串匹配的第一个位 ...
- [LeetCode] 552. 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 ...
随机推荐
- iOS开发资料
https://github.com/XCGit/awesome-objc-frameworks https://github.com/KevinHM/ios-good-practices-the-l ...
- HDU2612 BFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 , 一道比较简单的广搜(BFS)题目. 算法: 设置两个dist[][]数组,记录Y和M到几个K ...
- IDEA 编辑器如何将tabs 分行显示
https://jingyan.baidu.com/article/49ad8bcebd9e7c5834d8faac.html
- [VC]strcpy和strncoy的区别
第一种情况:char* p="how are you ?";char name[20]="ABCDEFGHIJKLMNOPQRS"; strcpy(name,p ...
- IOS 移除栈顶的控制器
- (IBAction)back2Two:(id)sender { // 移除当前栈顶的控制器 [self.navigationController popViewControllerAnimated ...
- 302和VS启动后网站拒绝访问的解决方案
网页状态302代表的是重定向的意思,就是网页跳转的一种状态 网站拒绝访问的时候可以在输出窗口查看是否有内容输出,如果没有说明启动网站的端口可能被占用,在网站项目——属性——web——项目中把地址的端口 ...
- 【POJ2774】Long Long Message(后缀数组求Height数组)
点此看题面 大致题意: 求两个字符串中最长公共子串的长度. 关于后缀数组 关于\(Height\)数组的概念以及如何用后缀数组求\(Height\)数组详见这篇博客:后缀数组入门(二)--Height ...
- 解决mysql8小时无连接自动断掉机制
windows下打开my.ini,增加: interactive_timeout=28800000 wait_timeout=28800000 MySQL是一个小型关系型数据库管理系统,由于MySQL ...
- Bootstrap 历练实例-轮播(carousel)插件方法
方法 下面是一些轮播(Carousel)插件中有用的方法: 方法 描述 实例 .carousel(options) 初始化轮播为可选的 options 对象,并开始循环项目. $('#identifi ...
- java基础面试题:java中实现多态的机制是什么?
靠的是父类或接口的引用指向子类或实现类的对象, 调用的方法是内存中正在运行的那个对象的方法.