【LeetCode】552. Student Attendance Record II
来源:互联网 发布:能看回放的网络电视 编辑:程序博客网 时间:2024/06/06 05:24
【LeetCode】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)
Note: The value of n won't exceed 100,000.
【输入输出】
Example 1:
Input: n = 2Output: 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.
【解题思路】
动态规划(DP):1. ans[n][a][l]表示总共n个字符,其中A字符最多有a个,结尾处最多有l个L字符2. 操作1:在结尾加P字符,ans[n][a][l] += ans[n-1][a][2] (如PLL结尾加P得到PLLP应该算入ans[4][0][0])3. 操作2:在结尾加A字符,ans[n][a][l] += ans[n-1][a-1][2]4. 操作3:在结尾加L字符,ans[n][a][l] += ans[n-1][a][l-1] (如PLL结尾不能加L)5. 返回ans[n][1][2]6. 由于n可取100,000,保存全部结果可能MLE,故加入flag参量,只保存n和n-1的结果。
动态规划(DP):1. ans[n][a][l]表示总共n个字符,其中A字符最多有a个,结尾处最多有l个L字符2. 操作1:在结尾加P字符,ans[n][a][l] += ans[n-1][a][2] (如PLL结尾加P得到PLLP应该算入ans[4][0][0])3. 操作2:在结尾加A字符,ans[n][a][l] += ans[n-1][a-1][2]4. 操作3:在结尾加L字符,ans[n][a][l] += ans[n-1][a][l-1] (如PLL结尾不能加L)5. 返回ans[n][1][2]6. 由于n可取100,000,保存全部结果可能MLE,故加入flag参量,只保存n和n-1的结果。
【代码】
class Solution {public:int checkRecord(int n) {const int con = (int)(1e9 + 7);int ans[2][2][3] = { { { 1, 1, 1 }, { 1, 1, 1 } } }, flag = 0;for (int i = 1; i <= n; i++) {flag = 1 - flag;for (int a = 0; a < 2; a++) {for (int l = 0; l < 3; l++) {ans[flag][a][l] = ans[1 - flag][a][2];if (a > 0) ans[flag][a][l] = (ans[flag][a][l] + ans[1 - flag][a - 1][2]) % con;if (l > 0) ans[flag][a][l] = (ans[flag][a][l] + ans[1 - flag][a][l - 1]) % con;}}}return ans[flag][1][2];}};
0 0
- 【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
- 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] DP之 Student Attendance Record II
- leetcode552. Student Attendance Record II
- LeetCode(六) DP 70.Climbing Stairs&552. Student Attendance Record II
- leetcode 552. Student Attendance Record II & 600. Non-negative Integers without Consecutive Ones
- leetcode 552. Student Attendance Record II 学生出勤记录 + 动态规划DP
- 高级Android到底有什么要求!
- c++的基本框架
- 【Docker】之 windows10 下 安装 Docker Tollbox
- 剑指offer(java代码)——从尾到头打印链表
- 删选法求素数
- 【LeetCode】552. Student Attendance Record II
- php综合web开发(2)
- 树形dp专题
- 僵尸进程
- php调用grep系统命令时出现Binary file (standard input) matches
- 动态规划——303. Range Sum Query
- python nditer---迭代数组
- 1001. A+B Format
- SCU 4438