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. 【Spark】必须要用CDH版本的Spark?那你是不是需要重新编译?

    目录 为什么要重新编译? 步骤 一.下载Spark的源码 二.准备linux环境,安装必须软件 三.解压spark源码,修改配置,准备编译 四.开始编译 为什么要重新编译? 由于我们所有的环境统一使用 ...

  2. c++11 符号修饰与函数签名、函数指针、匿名函数、仿函数、std::function与std::bind

    一.符号修饰与函数签名 1.符号修饰 编译器将c++源代码编译成目标文件时,用函数签名的信息对函数名进行改编,形成修饰名.GCC的C++符号修饰方法如下: 1)所有符号都以_z开头 2)名字空间的名字 ...

  3. leetcode-4. 寻找两个正序数组的中位数

    leetcode-4. 寻找两个正序数组的中位数. 给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2. 请你找出这两个正序数组的中位数,并且要求算法的时间复杂度为 O(l ...

  4. Git上传文件、文件夹到github

    上传一个文件: $ git add test.txt 上传多个文件: $ git add test.txt demo.txt 同理: 上传一个文件夹: $ git add learngit 上传多个文 ...

  5. 第一行Kotlin系列(三)Intent 向上一页返回数据onActivityResult的使用

    1.MainActivity.kt跳转处理 声明全局的按钮对象 private lateinit var button8: Button 实例化按钮对象 button8 = findViewById( ...

  6. [hdu5217]线段树

    题意:给定一个只含'(',')'的括号序列,有m个操作,改变某个位置的括号或者询问区间[L,R]内的括号进行配对后剩下的第K个括号的位置(配对的括号从原序列中删掉). 思路:首先对于一个括号序列,进行 ...

  7. PHPExcel生成Excel文件---提示导出文件或者文件扩展名不一致,或导出的文件或文件扩展名无效

    $data = Db::name('shop_cart')->where('phone','15555555555')->select(); $objPHPExcel = new PHPE ...

  8. 简单mysql存储过程

    直接上代码: CREATE DEFINER=`root`@`localhost` PROCEDURE `sos`( ) BEGIN -- 创建一个临时表 DROP TABLE IF EXISTS fi ...

  9. RocketMQ Windows 搭建

    一.rocketMQ 下载 官网:http://rocketmq.apache.org/ 本人使用是v4.3.0版本,百度网盘下载地址链接:https://pan.baidu.com/s/1qWewB ...

  10. 如何发布自己用python写的py模块

    Python——怎么发布你的Python模块 我们在学习Python的时候,除了用pip安装一些模块之外,有时候会从网站下载安装包下来安装,我也想要把我自己编写的模块做成这样的安装包,该怎么办,如何发 ...