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.

Approach #1: DP. [C++]

class Solution {
public:
int checkRecord(int n) {
int m = 1000000007;
int *A = new int [n+5];
int *P = new int [n+5];
int *L = new int [n+5]; P[0] = 1;
L[0] = 1;
L[1] = 3;
A[0] = 1;
A[1] = 2;
A[2] = 4; if(n == 1) return 3; for(int i = 1; i < n; i++)
{
A[i - 1] %= m;
P[i - 1] %= m;
L[i - 1] %= m; P[i] = ((A[i - 1] + P[i - 1]) % m + L[i - 1]) % m; if(i > 1) L[i] = ((A[i - 1] + P[i - 1]) % m + (A[i - 2] + P[i - 2]) % m) % m; if(i > 2) A[i] = ((A[i - 1] + A[i - 2]) % m + A[i - 3]) % m;
} return ((A[n - 1] % m + P[n - 1] % m) % m + L[n - 1] % m) % m;
}
};

At the first time i initial A, P, L arrays with n, the compiler hint that AddressSanitizer: heap-buffer-overflow on address. After i modified n to n+5, it compiled successfully.

Analysis:

1. Thinking process

1.1 Divide the whole problem into sub-problems:

Before introducing the way to calculate the number of all possible attendance records with length n, we divide the problem into 3 part.

As the attendance records is made by 3 characters('P', 'L', 'A'), the total number can be divided into

Total = ended with P + ended with L + ended with A.

If we define following series

T(n) is the total number of all possible attendance records with length n.

P(n) is the total number of all possible attendance records with length n.

L(n) is the total number of all possible attendance records with length n.

A(n) is the total number of all possible attendance records with length n.

It can be inferred that

T(n) = A(n) + P(n) + L(n), n >= 1

1.2 Solve the sub-problems by dynamic programming

As I use dynamic programming, I need to find out the recurive relaion in 3 sub-problems.

2.2.1 Calculate P(n)

It can be inferred that

If we add a 'P' to an attendance records with length n-1, we will get an attendance records ended with 'P' with length n.

For an attendance record with length n-1.

  • If its (n-1)th character is 'P' ---- CAN add 'P'. ('PP')
  • If its (n-1)th character is 'A' ---- CAN add 'P'. ('AP')
  • If its (n-1)th character is 'L' ---- CAN add 'P'. ('LP')

which means:

P(n) = A(n-1) + P(n-1) + L(n-1), n >= 2.

and we have initial value for the recursive relation

A(1) = P(1) = L(1) = 1.

1.2.2 Calculate L(n)

Similary,

If we add a 'L' to an attendance records with length n-1, we will get an attendance records ended with 'L' with length n.

But the resulting attendance records must be regarded as rewardable!

As the rule is that a record is regarded as rewardable if it doesn't contain.

more than two continuous 'L' (late).

We need to consider the situations when we can add 'L' to an attendance record with length n-1 and it's still regarded as rewardable.

For an attendance record with length n-1.

  • If its (n-1)th character is 'P' ---- CAN add 'L'. ('PL')
  • If its (n-1)th character is 'A' ---- CAN add 'L'. ('AL')

  • If its (n-1)th character is 'L':

    • If its (n-2)th character is 'A' ---- CAN add 'L'. ('ALL')
    • If its (n-2)th character is 'P' ---- CAN add 'L'. ('PLL')
    • If its (n-2)th character is 'L' ---- CAN NOT add 'L'. ('LLL' breaks the rule).

which means:

L(n) = A(n-1) + P(n-1) + A(n-2) + P(n-2), n>= 3.

and we have initial value for the recursive relation

A(1) = P(1) = 1

A(2) = 2, P(2) = 3

and

L(1) = 1, L(2) = 3

1.2.3 Calculate A(n).

Similary,

If we add a 'A' to an attendance records with length n-1, we will get an attendance records endeds with 'A' with length n.

But resulting attendance records must be regarded as rewardable!

As the rule is that a record is regarded as rewardable if it doesn't contain

more than one 'A' (absent)

We need to consider the situations when we can add 'A' to an attedance record with length n-1 and it's still regarded as rewardable.

For an attendance record with length n-1.

  • If its (n-1)th character is 'A' ---- CAN NOT add 'A'. ("AA" break the rule)
  • If its (n-1)th character is 'P' and has no 'A' ---- CAN add 'A'.
  • If its (n-1)th character is 'L' and has no 'A' ---- CAN add 'A'.

If we define series

noAP(n) is the total number of all possible attendance records ended with 'P' with length n and with no 'A'.

noAL(n) is the total number of all possible attendance records ended with 'L' with length n and with no 'A'.

It can be inferred that

A(n) = noAP(n-1) + noAL(n-1), n>= 2.

and we have initial value for the recursive relation

A(1) = 1

noAP(1) = noAL(1) = 1

1.2.4 Calculate noAP(n) and noAL(n)

In 2.2.3, 2 new series noAP(n) and noAL(n) is introduced. Now we focus on the recursive relation in noAP(n) and noAL(n).

For noAP(n), we need to consider the situation when we can add 'P' to an attendance record with length n-1 and no 'A' and it's still regarded as rewardable.

Since noAP(n) has no 'A', we don't need to consider the situation when its (n-1)th character is 'A'.

For an attendance record with length n-1, we can get only 2 situations

  • If its (n-1)th character is 'P' and has no 'A' ---- CAN add 'P'.
  • If its (n-1)th character is 'L' and has no 'A' ---- CAN add 'P'.

which means

noAP(n) = noAP(n-1) + noAP(n-1), n>= 2.

and we have initial value for the recursive relation

noAP(1) = noAL(1) = 1.

For noAL(n), we need to consider the situations when we can add 'L' to an attendance record with length n-1 and no 'A' ans it's still regraded as rewardable.

Since noAL(n) has no 'A', we don'r need to consider the situation when its (n-1)th character is 'A'.

For an attendance record with length n-1, we can get

  • If its (n-1)th character is 'P' and has no 'A' ---- CAN add 'L'. ("PL")
  • If its (n-1)th character is ‘L' and has no 'A'.
    • If its (n-2)th character is 'P' and has no 'A' ---- CAN add 'L'.
    • If its (n-2)th character is 'L' and has no 'A' ---- CAN NOT  add 'L'.

which mean

noAL(n) = noAP(n-1) + noAP(n-2), n>= 3.

and we have initial value for the recursive relation

noAP(1) = noAL(1) = 1.

and

noAL(2) = 2

1.3 Recursive relationship summarizaion.

The answer to the whole problem is T(n), and

T(n) = A(n) + P(n) + L(n), n >= 1.

Recursive formula:

P(n) = A(n - 1) + P(n - 1) + L(n - 1), n ≥ 2.

A(n) = noAP(n - 1) + noAL(n - 1), n ≥ 2.

noAP(n) = noAP(n - 1) + noAL(n - 1), n ≥ 2.

L(n) = A(n - 1) + P(n - 1) + A(n - 2) + P(n - 2), n ≥ 3.

noAL(n) = noAP(n - 1) + noAP(n - 2), n ≥ 3.

with initial value

A(1) = P(1) = L(1) = 1.

noAP(1) = noAL(1) = 1.

L(2) = 3.

noAL(2) = 2.

1.4 Simplifying.

When n >= 4, the 3 formulas

A(n) = noAP(n - 1) + noAL(n - 1), n ≥ 2.

noAP(n) = noAP(n - 1) + noAL(n - 1), n ≥ 2.

noAL(n) = noAP(n - 1) + noAP(n - 2), n ≥ 3.

can be simplified to

A(n) = A(n - 1) + A(n - 2) + A(n - 3), n ≥ 4.

Finally, the recursive formula group becomes

P(n) = A(n - 1) + P(n - 1) + L(n - 1), n ≥ 2.

L(n) = A(n - 1) + P(n - 1) + A(n - 2) + P(n - 2), n ≥ 3.

A(n) = A(n - 1) + A(n - 2) + A(n - 3), n ≥ 4.

Here, noAP(n) and noAL(n) disappeared.

with initial value

P(1) = 1.

L(1) = 1, L(2) = 3.

A(1) = 1, A(2) = 2, A(3) = 4.

1.5 Do modulus

The result need to be return after mod 10^9 + 7.

Since the result is generated by adding a lot of middle results together. in order to make sure that every middle result and the final result won't exceed INT_MAX, we need to do modulus for every middle result. and for every 2-middle-result-addition.

2. Complexity analysis

2.1 Time complexity

Since the algorithm is one-pass from 1 to n.

The time complexity is O(n).

2.2 Space Complexity

Since 3 array are used P(n), L(n), A(n), the total size is 3n.

The space complexity is O(n).

Reference:

https://leetcode.com/problems/student-attendance-record-ii/discuss/101643/Share-my-O(n)-C%2B%2B-DP-solution-with-thinking-process-and-explanation

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

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

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

  2. 【leetcode】552. Student Attendance Record II

    题目如下: Given a positive integer n, return the number of all possible attendance records with length n ...

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

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

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

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

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

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

  6. 551. Student Attendance Record I + Student Attendance Record II

    ▶ 一个学生的考勤状况是一个字符串,其中各字符的含义是:A 缺勤,L 迟到,P 正常.如果一个学生考勤状况中 A 不超过一个,且没有连续两个 L(L 可以有多个,但是不能连续),则称该学生达标(原文表 ...

  7. 551. Student Attendance Record I 从字符串判断学生考勤

    [抄题]: You are given a string representing an attendance record for a student. The record only contai ...

  8. 551. Student Attendance Record I【easy】

    551. Student Attendance Record I[easy] You are given a string representing an attendance record for ...

  9. 【leetcode_easy】551. Student Attendance Record I

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

随机推荐

  1. ICaptureGraphBuilder2::RenderStream 智能连接方法浅析

    ICaptureGraphBuilder2::RenderStream HRESULT RenderStream( [in] const GUID *pCategory, [in] const GUI ...

  2. chrome36可以使用自定义元素的回调了

    <!DOCTYPE html> <html> <head> <title>ms-attr-*</title> <meta charse ...

  3. awk编程基础

    一.awk介绍 awk(名字来源于三个创始人姓氏首字母)是linux系统下文本编辑工具,是一门编程语言,有自己的基本语法和流程控制.函数.awk简单高效.   二.awk的运行方法 例子:使用冒号:分 ...

  4. DataGuard的cascading standby(1拖N的模式)

    在Oracle11.2.0.2版本后,dataguard支持级联模式传输日志,即日志传输可以从A到B,B到C,B到D,等等,无穷无尽 cascading standby可以分担主库传输日志到多个备库的 ...

  5. java高级工程师(一)

    一.无笔试题   不知道是不是职位原因还是没遇到,面试时,都不需要做笔试题,而是填张个人信息表格,或者直接面试     二.三大框架方面问题   1.Spring 事务的隔离性,并说说每个隔离性的区别 ...

  6. springMVC入门程序。使用springmvc实现商品列表的展示。

    1.1 开发环境 本教程使用环境: Jdk:jdk1.7.0_72 Eclipse:mars Tomcat:apache-tomcat-7.0.53 Springmvc:4.1.3 1.2 需求 使用 ...

  7. 使用mybatis提供的各种标签方法实现动态拼接Sql。使用sql片段提取重复的标签内容

    Sql中可将重复的sql提取出来,使用时用include引用即可,最终达到sql重用的目的,如下: <select id="findUserByNameAndSex" par ...

  8. Python爬虫利器六之PyQuery的用法

    前言 你是否觉得 XPath 的用法多少有点晦涩难记呢? 你是否觉得 BeautifulSoup 的语法多少有些悭吝难懂呢? 你是否甚至还在苦苦研究正则表达式却因为少些了一个点而抓狂呢? 你是否已经有 ...

  9. JQuery 对象和事件

    JQuery 对象和事件 一:JQuery 对象和 Dom 对象 在使用 JQuery 过程中,我们一般(也是绝大多数情况下,除非我们使用了第二个框架)只有两类对象,即:JQuery 对象和 Dom ...

  10. 如何用Python实现常见机器学习算法-3

    三.BP神经网络 1.神经网络模型 首先介绍三层神经网络,如下图 输入层(input layer)有三个units(为补上的bias,通常设为1) 表示第j层的第i个激励,也称为单元unit 为第j层 ...