552. 学生出勤记录 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。

先附上大佬用数学方法做的:

用数组放入关系,太顶了

下面才是我写的勉强过关的代码

class Solution {
public int checkRecord(int n) {
long[][] a = new long[][]{{1},{1},{0},{1},{0},{0}};
long[][] aMatrix = new long[][]{{1,1,1,0,0,0},{1,0,0,0,0,0},{0,1,0,0,0,0},{1,1,1,1,1,1},{0,0,0,1,0,0},{0,0,0,0,1,0}};
while (n>0) {
int m = n & 1;
if (m == 1) {
a = this.multipleMatrix(aMatrix, a);
}
aMatrix = this.multipleMatrix(aMatrix, aMatrix);
n = n>>1;
}
/**
* 0 A0L0
* 1 A0L1
* 2 A0L2
* 3 A1L0
* 4 A1L1
* 5 A1L2
*/
return (int)a[3][0]; }
public long[][] multipleMatrix(long[][] a,long[][] b) {
long mod = (long)1e9+7;
long c[][] = new long[a.length][b[0].length];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b[i].length; j++) {
for (int k = 0; k < a[i].length; k++) {
c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;
}
}
}
return c;
}
}
class Solution {
long M = 1000000007;
public int checkRecord(int n) {
long[] f = new long[n <= 5 ? 6 : n + 1];
f[0] = 1;
f[1] = 2;
f[2] = 4;
f[3] = 7;
//四种情况下,我前三个可奖励,我最后一个是l是p无所谓
//如果中间出现两个LL,那么我必定无效
//2*f[i-1]和一个-f[i-4]
for (int i = 4; i <= n; i++)
f[i] = ((2 * f[i - 1]) % M + (M - f[i - 4])) % M;
long sum = f[n];
for (int i = 1; i <= n; i++) {
sum += (f[i - 1] * f[n - i]) % M;
}
return (int)(sum % M);
} }

Java实现 LeetCode 552 学生出勤记录 II(数学转换?还是动态规划?)的更多相关文章

  1. Leetcode 552.学生出勤记录II

    学生出勤记录II 给定一个正整数 n,返回长度为 n 的所有可被视为可奖励的出勤记录的数量. 答案可能非常大,你只需返回结果mod 109 + 7的值. 学生出勤记录是只包含以下三个字符的字符串: ' ...

  2. Java实现 LeetCode 551 学生出勤记录 I(暴力大法好)

    551. 学生出勤记录 I 给定一个字符串来代表一个学生的出勤记录,这个记录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : Present,到场 如果一个 ...

  3. 552 Student Attendance Record II 学生出勤记录 II

    给定一个正整数 n,返回长度为 n 的所有可被视为可奖励的出勤记录的数量. 答案可能非常大,你只需返回结果mod 109 + 7的值.学生出勤记录是只包含以下三个字符的字符串:    1.'A' : ...

  4. [Swift]LeetCode552. 学生出勤记录 II | Student Attendance Record II

    Given a positive integer n, return the number of all possible attendance records with length n, whic ...

  5. 力扣(LeetCode)学生出勤记录I 个人题解

    给定一个字符串来代表一个学生的出勤记录,这个记录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : Present,到场 如果一个学生的出勤记录中不超过一个' ...

  6. 551.学生出勤记录I

    /* * @lc app=leetcode.cn id=551 lang=java * * [551] 学生出勤记录 I * * https://leetcode-cn.com/problems/st ...

  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. [LeetCode] Student Attendance Record II 学生出勤记录之二

    Given a positive integer n, return the number of all possible attendance records with length n, whic ...

  9. Java for LeetCode 092 Reverse Linked List II

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1-> ...

随机推荐

  1. Try-Catch包裹的代码异常后,竟然导致了产线事务回滚!

    导读:​一段被try-catch包裹后的代码在产线稳定运行了200天后忽然发生了异常,而这个异常竟然导致了产线事务回滚.这期间究竟发生了什么?日常在项目过程中该如何避免事务异常?就在这个时候,老板拿着 ...

  2. shell 条件结构之 if 语句使用总结

    文章目录 #条件判断的格式 [ exp ] [[ exp ]] test exp 注意: exp 与 "["."]"括号之间必须要有空格,否则会报语法错误: [ ...

  3. USACO 3.1 Contact

    http://www.nocow.cn/index.php/Translate:USACO/contact 题目大意:给一个只含0和1的序列,统计每个子序列的重复次数,并按次数递减来输出 考虑子序列时 ...

  4. sql 取值时将字符串类型数字转化成整形

    select cast(a.Vchcode as int) as avchcode,a.ptypeid,a.assqty,unit,b.pfullname,b.standard,b.type from ...

  5. Docker & k8s 系列二:本机k8s环境搭建

    本篇将会讲解k8s是什么?本机k8s环境搭建,部署一个pod并演示几个kubectl命令,k8s dashboard安装. k8s是什么 k8s是kubernetes的简写,它是一个全新的基于容器技术 ...

  6. python中的基础坑

    v = [lambda :x for x in range(10)] print(v) #[lambda :x,lambda :x....]10个匿名函数 print(v[0]) #lambda :x ...

  7. RBAC在Django中基于中间件的AJAX应用案例

    项目文件:   models.py from django.db import models from django.contrib.auth.models import AbstractUser # ...

  8. day06:三级菜单练习0218

    #1:省份数列:data = { "北京":{ "昌平":{ "沙河":["oldboy","电信" ...

  9. vscode环境配置(一)——C Program运行

    ctrl + shift +p 打开应用商店 搜索 C/C++  和 Code Runner(一键编译运行)  

  10. 使用vue2.0创建的项目的步骤

    1.由于vue项目依赖 node.js npm 需要先安装.   若没有请先安装,请百度 //检查是否有node.js  npm vue win+r   输入cmd  输入node -v  回车 会出 ...