Java实现 LeetCode 552 学生出勤记录 II(数学转换?还是动态规划?)
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(数学转换?还是动态规划?)的更多相关文章
- Leetcode 552.学生出勤记录II
学生出勤记录II 给定一个正整数 n,返回长度为 n 的所有可被视为可奖励的出勤记录的数量. 答案可能非常大,你只需返回结果mod 109 + 7的值. 学生出勤记录是只包含以下三个字符的字符串: ' ...
- Java实现 LeetCode 551 学生出勤记录 I(暴力大法好)
551. 学生出勤记录 I 给定一个字符串来代表一个学生的出勤记录,这个记录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : Present,到场 如果一个 ...
- 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 ...
- 力扣(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 ...
- [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 ...
- 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-> ...
随机推荐
- python路径操作新标准:pathlib 模块
之前如果要使用 python 操作文件路径,我总是会条件反射导入 os.path. 而现在,我会更加喜欢用新式的 pathlib, 虽然用得还是没有 os.path 熟练,但是以后会坚持使用. pat ...
- go实现SnowFlake
package main import ( "errors" "fmt" "strconv" "sync" " ...
- angular js 页面修改数据存入数据库
一.编写service,修改数据要根据ID回显数据 //根据ID查询 public Brand findById(Long id); //修改 public int update(Brand bran ...
- Xshell 与 Xftp 的安装与使用
我们在日常工作中,不管是系统管理员.程序员.还是技术工程师,如果想登陆到 Linux 服务器,不可能总是跑到机房去工作,通常我们需要一个工具帮我们去做远程连接,这样我们只需要用笔记本电脑就可以连接到服 ...
- 「雕爷学编程」Arduino动手做(40)——旋转编码器模块
37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里 ...
- class.getFields和class.getDeclareFields的区别
class.getFields的定义 返回类提供的public域包括超类的共有变量; 注: 是public,我们平时定义变量一般用的private,如果用getFields是不会获得. class.g ...
- DPDK Timer Library原理(学习笔记)
0 前置知识学习跳表(SkipList) 跳表应具有以下特征: 1)一个跳表应该有多个层(level)组成,通常是10-20层. 2)跳表的第0层包含所有的元素. 3)每一层都是一个有序的链表.层数越 ...
- SICP 题解集合
1.1(略) 1.2 biwascheme> (/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5))))) (* 3 (- 6 2) (- 2 7))) => -0.24666 ...
- HDU3746 Cyclic Nacklace
题目链接:https://vjudge.net/problem/HDU-3746 知识点: KMP 解题思路: 论如何用 \(Next[]\) 数组求循环节. AC代码: #include <b ...
- 【XSS-labs】Level 1-5
写在前面: 这个闯关游戏旨在理解XSS的原理并运用各种姿势绕过对于XSS攻击的过滤和限制. 这个练习只要弹出弹框即可过关 ,每一关我也会附上payload和源代码的解析 Level 1 观察源码 &l ...