Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
LeetCode/#Student Attendance Record II
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
46 lines (38 sloc)
1.3 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
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: | |
'A' : Absent. | |
'L' : Late. | |
'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. | |
``` | |
My code: | |
```python | |
class Solution(object): | |
def checkRecord(self, n): | |
""" | |
:type n: int | |
:rtype: int | |
""" | |
if n == 1: | |
return 3 | |
dp = [1, 1, 2] | |
M = 10 ** 9 + 7 | |
for i in range(2, n+1): | |
dp.append((dp[i] + dp[i-1] + dp[i-2]) % M) | |
res = 0 | |
for i in range(n+1): | |
# i represent the length before 'A' ,(n+1) - 1 - i means the length after 'A' | |
res += (dp[i+1] * dp[(n+1) - 1 - i]) % M | |
res %= M | |
return res | |
``` | |
Solution: | |
https://leetcode.com/problems/student-attendance-record-ii/ |