Leetcode-552 Student Attendance Record II(学生出勤记录 II)

 1 #define maxn 1000000
 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
 3 #define pb push_back
 4 #define MOD 1000000007
 5
 6 class Solution
 7 {
 8     public:
 9         int checkRecord(int n)
10         {
11             long long int dp[2][2][3] {0};
12
13             dp[0][0][0] = dp[0][0][1] = dp[0][1][0] = 1;
14
15             _for(i,2,n+1)
16             {
17                 if(!(i&0x1))
18                 {
19                     dp[1][0][0] = (dp[0][0][0]+dp[0][0][1]+dp[0][0][2])%MOD;
20                     dp[1][0][1] = dp[0][0][0];
21                     dp[1][0][2] = dp[0][0][1];
22                     dp[1][1][0] = ((dp[0][0][0]+dp[0][0][1])%MOD+(dp[0][0][2]+dp[0][1][0])%MOD+(dp[0][1][1]+dp[0][1][2])%MOD)%MOD;
23                     dp[1][1][1] = dp[0][1][0];
24                     dp[1][1][2] = dp[0][1][1];
25                 }
26                 else
27                 {
28                     dp[0][0][0] = (dp[1][0][0]+dp[1][0][1]+dp[1][0][2])%MOD;
29                     dp[0][0][1] = dp[1][0][0];
30                     dp[0][0][2] = dp[1][0][1];
31                     dp[0][1][0] = ((dp[1][0][0]+dp[1][0][1])%MOD+(dp[1][0][2]+dp[1][1][0])%MOD+(dp[1][1][1]+dp[1][1][2])%MOD)%MOD;
32                     dp[0][1][1] = dp[1][1][0];
33                     dp[0][1][2] = dp[1][1][1];
34                 }
35             }
36             if(!(n&0x1))
37                 n = 1;
38             else
39                 n = 0;
40             int rnt = (dp[n][0][0]+dp[n][0][1]+dp[n][0][2]
41             +dp[n][1][0]+dp[n][1][1]+dp[n][1][2])%MOD;
42
43             return rnt;
44         }
45 };

原文地址:https://www.cnblogs.com/Asurudo/p/10375237.html

时间: 2024-08-29 15:06:53

Leetcode-552 Student Attendance Record II(学生出勤记录 II)的相关文章

LeetCode 551. Student Attendance Record I (C++)

题目: You are given a string representing an attendance record for a student. The record only contains the following three characters: 'A' : Absent. 'L' : Late. 'P' : Present. A student could be rewarded if his attendance record doesn't contain more th

552. Student Attendance Record II

Problem refer: https://leetcode.com/problems/student-attendance-record-ii/description // My solution: // A good mathmatical derivation problem. // But it makes me confused with the word: "more than two continuous 'L' (late)". // And finally find

551. Student Attendance Record I【easy】

551. Student Attendance Record I[easy] You are given a string representing an attendance record for a student. The record only contains the following three characters: 'A' : Absent. 'L' : Late. 'P' : Present. A student could be rewarded if his attend

【leetcode_easy】551. Student Attendance Record I

problem 551. Student Attendance Record I 参考 1. Leetcode_easy_551. Student Attendance Record I; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/10947954.html

551. 学生出席记录 Student Attendance Record I

You are given a string representing an attendance record for a student. The record only contains the following three characters: 'A' : Absent. 'L' : Late. 'P' : Present. A student could be rewarded if his attendance record doesn't contain more than o

[LeetCode] Student Attendance Record I

You are given a string representing an attendance record for a student. The record only contains the following three characters: 'A' : Absent. 'L' : Late. 'P' : Present. A student could be rewarded if his attendance record doesn't contain more than o

551. Student Attendance Record I(LeetCode)

You are given a string representing an attendance record for a student. The record only contains the following three characters: 'A' : Absent. 'L' : Late. 'P' : Present. A student could be rewarded if his attendance record doesn't contain more than o

551 Student Attendance Record I

You are given a string representing an attendance record for a student. The record only contains the following three characters: 'A' : Absent. 'L' : Late. 'P' : Present. A student could be rewarded if his attendance record doesn't contain more than o

力扣(LeetCode)学生出勤记录I 个人题解

给定一个字符串来代表一个学生的出勤记录,这个记录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : Present,到场 如果一个学生的出勤记录中不超过一个'A'(缺勤)并且不超过两个连续的'L'(迟到),那么这个学生会被奖赏. 你需要根据这个学生的出勤记录判断他是否会被奖赏. 示例 1: 输入: "PPALLP" 输出: True 示例 2: 输入: "PPALLL" 输出: False 做法比较清晰,当A的次数大于1时,