题目如下:

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:

  1. Input: n = 2
  2. Output: 8
  3. Explanation:
  4. There are 8 records with length 2 will be regarded as rewardable:
  5. "PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL"
  6. 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位的出席记录数总和,再加上第一种情况的个数,即为最后的结果。

代码如下:

  1. class Solution(object):
  2. def checkRecord(self, n):
  3. """
  4. :type n: int
  5. :rtype: int
  6. """
  7. MOD = pow(10,9) + 7
  8. if n <= 1:
  9. return [0,3][n]
  10. dp = []
  11. for i in range(n): #0:L,1:P
  12. dp.append([0]*2)
  13. dp[0][0] = dp[0][1] = 1
  14. dp[1][0] = dp[1][1] = 2
  15. for i in range(2,n):
  16. dp[i][0] = (dp[i-2][0] % MOD + (dp[i-2][1]*2) % MOD) % MOD
  17. dp[i][1] = (dp[i-1][0] % MOD + dp[i-1][1] % MOD) % MOD
  18. res = 0
  19. for i in range(n):
  20. if i == 0 or i == n - 1:
  21. res += (dp[n-2][0] + dp[n-2][1])
  22. else:
  23. res += (dp[i-1][0] + dp[i-1][1]) * (dp[n-i-2][0] + dp[n-i-2][1])
  24. 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. .NET面试题集锦①

    一.前言部分 文中的问题及答案多收集整理自网络,不保证100%准确,还望斟酌采纳. 1.面向对象的思想主要包括什么? 答:任何事物都可以理解为对象,其主要特征: 继承.封装.多态.特点:代码好维护,安 ...

  2. 51nod 1384:全排列(STL)

    题目链接 记住next_permutation函数的用法,另外string在这里比char[]慢好多啊.. //#include<bits/stdc++.h> //using namesp ...

  3. SpringMvc Filter的使用

    一:Filter过滤器. 先自定义一个过滤器. package com.jbj.filter; import org.springframework.web.filter.OncePerRequest ...

  4. exists 的简单介绍

    准备数据: CREATE TABLE Books( BookID number, BookTitle VARCHAR2(20) NOT NULL, Copyright varchar2(20) ) I ...

  5. php nl2br()函数 语法

    php nl2br()函数 语法 作用:在字符串中的新行(\n)之前插入换行符dd马达 语法:nl2br(string,xhtml) 参数: 参数 描述 string 必须.规定要检查的字符串. xh ...

  6. JAVA(JDK,JRE)更改目录安装及环境变量配置

    重温一下 JAVA(JDK,JRE)更改目录安装及环境变量配置 https://jingyan.baidu.com/article/e2284b2b5b7ae5e2e7118d11.html 备注:随 ...

  7. 【Java架构:持续交付】一篇文章搞掂:持续交付理论

    一.持续集成.持续交付.DevOps概念,关系等 持续集成(Continuous integration/CI) 持续交付(Continuous delivery/CD) 持续部署() 持续 (Con ...

  8. 2018-2019-2 20175307实验三《敏捷开发与XP实践》实验报告

    实验三 敏捷开发与XP实践-1 1.仔细学习了http://www.cnblogs.com/rocedu/p/4795776.html,发布了一篇关于Google的Java编码的博客,具体内容就不在这 ...

  9. dubbo 漫谈一

    转:腾信视频 阿甘 https://ke.qq.com/course/216518 https://blog.csdn.net/xlgen157387/article/details/51865289 ...

  10. 日期和时间格式(ISO 8601)

    参考 ISO 8601 - Wikipedia ISO 8601 Date and time format