552. Student Attendance Record II
来源:互联网 发布:吴用 知乎 编辑:程序博客网 时间:2024/06/06 02:46
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:
‘A’ : Absent.
‘L’ : Late.
‘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.
题目大意:
学生出席记录,A表示缺席,L表示迟到,P表示出席,不能有两次缺席,不能有连续三次迟到,找到长度为n且满足上述条件的出席表的个数。
思路:
动态规划题目
我们用p[n][a][l]来表示,长度为n且缺席次数最多为a连续迟到次数最多为l的个数
则p[i][a][l] = p[i-1][a][2] //只能是一个P
+ p[i-1][a-1][2] //可以是一个A
+ p[i-1][a][k-1] //可以是一个L
依次类推可以得到答案
class Solution {public: int checkRecord(int n) { int MOD = 1000000007; int f[n+1][2][3]; memset(f,0,sizeof(f)); for(int i =0 ;i<=n;i++){ for(int j=0;j<2;j++){ for(int k=0;k<3;k++){ f[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 = f[i-1][j][2];//P only if(j>0) val = (val+f[i-1][j-1][2])%MOD; //Could be A if(k>0) val = (val+f[i-1][j][k-1])%MOD; //Could be L f[i][j][k] = val; } } } return f[n][1][2]; }};
- 552. Student Attendance Record II
- 552. Student Attendance Record II
- 552. Student Attendance Record II
- 552. Student Attendance Record II
- 552. Student Attendance Record II
- 552. Student Attendance Record II
- 【LeetCode】552. Student Attendance Record II
- [LeetCode]552. Student Attendance Record II
- LeetCode 552. Student Attendance Record II
- leetcode 552. Student Attendance Record II
- leetcode#552. Student Attendance Record II
- [leetcode]552. Student Attendance Record II
- leetcode552. Student Attendance Record II
- [Leetcode] 552. Student Attendance Record II 解题报告
- leetcode_552.Student Attendance Record II ? 待解决
- [LeetCode] DP之 Student Attendance Record II
- Student Attendance Record I
- Student Attendance Record I
- C# 单例模式
- 用到的C++语法
- eclipse导出可直接运行的jar项目
- Destroy 和 DestroyImmediate 使用区别
- linux常用命令sort,uniq,cut,wc
- 552. Student Attendance Record II
- CSS三种样式
- model quantization
- windows配置logstash java环境
- CentOS6.5final下RabbitMQ3.6.10的安装和使用
- python None和Null
- 拼多多前端笔试题(二)
- &和&&的区别(与) 和 ||和|的区别(或)
- 查看ubuntu下Qt的版本