题目如下:

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:

  1. 'A' : Absent.
  2. 'L' : Late.
  3. '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.

Note: The value of n won't exceed 100,000.

解题思路:根据题目要求,A最多可以出现1次,同时L不能连续出现三个及以上。显然,所有符合条件的出席记录 = 不包含A的出席记录数 + 包含A的出席记录数。首先来看不包含A的出席记录数怎么求,假设dp[i][0]和dp[i][1]分别表示第i个元素为L和为P时候可以构成的符合条件的出席记录数,那么有dp[i][1] = dp[i-1][0] + dp[i-1][1],因为如果第i位是P,那么i-1是L或者是P都是允许的;同时有 dp[i][0] = dp[i-2][0]  + dp[i-2][1]*2,这是因为如果第i位是L,如果i-2位也是L的话,则第i-1位就只能是P,而i-2是P的话,第i-2位是L或者是P都是允许的。接下来看包含A的情况,假设A放在出席记录的第i位的位置,出席记录就会被分割成[0:i-1]和[i+1:n]两段,这两段也只能包含L和P,所以正好又可以转化为第一种情况中已经计算出来的dp[i-1]和dp[n-i]两种,A在第i位符合条件的出席记录数就是dp[i-1]*dp[n-i],依次累计A在第0位~第N-1位的出席记录数总和,再加上第一种情况的个数,即为最后的结果。

代码如下:

class Solution(object):
def checkRecord(self, n):
"""
:type n: int
:rtype: int
"""
MOD = pow(10,9) + 7
if n <= 1:
return [0,3][n]
dp = []
for i in range(n): #0:L,1:P
dp.append([0]*2)
dp[0][0] = dp[0][1] = 1
dp[1][0] = dp[1][1] = 2
for i in range(2,n):
dp[i][0] = (dp[i-2][0] % MOD + (dp[i-2][1]*2) % MOD) % MOD
dp[i][1] = (dp[i-1][0] % MOD + dp[i-1][1] % MOD) % MOD
res = 0
for i in range(n):
if i == 0 or i == n - 1:
res += (dp[n-2][0] + dp[n-2][1])
else:
res += (dp[i-1][0] + dp[i-1][1]) * (dp[n-i-2][0] + dp[n-i-2][1])
return (res + dp[-1][0] + dp[-1][1]) % (pow(10, 9) + 7)

【leetcode】552. Student Attendance Record II的更多相关文章

  1. 【LeetCode】551. Student Attendance Record I 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则表达式 统计 日期 题目地址:https://l ...

  2. [LeetCode] 552. Student Attendance Record II 学生出勤记录之二

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

  3. 552. Student Attendance Record II

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

  4. 【leetcode_easy】551. Student Attendance Record I

    problem 551. Student Attendance Record I 题意: solution: class Solution { public: bool checkRecord(str ...

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

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

  6. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  7. [LeetCode] Student Attendance Record II 学生出勤记录之二

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

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

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

  9. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

随机推荐

  1. mybatis用distinct进行查询的问题

    一:mybatis进行distinct进行查询的时候,数据库中可能有值为null的. 如果直接这样写,这个null的都给计算出来了. select DISTINCT b.b_city from bui ...

  2. php strnatcmp()函数 语法

    php strnatcmp()函数 语法 作用:自然顺序法比较字符串直线往复电机 语法:strnatcmp(string1,string2) 参数: 参数 描述 string1 必须,规定要比较的第一 ...

  3. centos7下安装storm步骤

      前言 真是后知后觉,最近忙也要学习,把以前丢的都要拾起来.原理懂不懂也把环境搭起来学习.   环境  centos7 jdk 1.8 zookeeper 3.4.13 storm 1.2.2 安装 ...

  4. SSH弱小算法支持问题

    SSH弱小算法支持问题:SSH的配置文件中加密算法没有指定(没有配置加密算法),则会默认支持所有加密算法,包括arcfour,arcfour128,arcfour256等弱加密算法.解决方法:1.修改 ...

  5. php7结合mongoDB插入数据

    php7结合mongoDB插入数据 代码如下: <?php $bulk = new MongoDB\Driver\BulkWrite;//1 $document = ['_id' => n ...

  6. [CSP-S模拟测试]:小奇的矩阵(matrix)(DP+数学)

    题目背景 小奇总是在数学课上思考奇怪的问题. 题目描述 给定一个$n\times m$的矩阵,矩阵中的每个元素$a_{i,j}$为正整数.接下来规定:    $1.$合法的路径初始从矩阵左上角出发,每 ...

  7. 前端每日实战:38# 视频演示如何用纯 CSS 创作阶梯文字特效

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/MXYBEM 可交互视频教程 此视频 ...

  8. docstoc对Scribd的威胁比SlideShare还要大。

    docstoc,这是在TechCrunch40互联网交流会上崭露头角的个性化服务.docstoc为用户提供了在线存储.分享以及交流文档的互联网服务.与Scribd相同的是,除了文档分享功能以外,doc ...

  9. WEUI官方样式小程序工具打开预览

    https://github.com/Tencent/weui-wxss 用微信web开发者工具打开dist目录(请注意,是dist目录,不是整个项目)

  10. Appium初始化设置:手写代码连接手机、appium-desktop连接手机

    一.包名获取的三种方式 1)找开发要2)mac使用命令:adb logcat | grep START win使用命令:adb logcat | findstr START 或者可以尝试使用第3条命令 ...