Student Attendance Record II Problem
Description
LeetCode Problem 552.
An attendance record for a student can be represented as a string where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:
- 'A': Absent.
- 'L': Late.
- 'P': Present.
Any student is eligible for an attendance award if they meet both of the following criteria:
- The student was absent ('A') for strictly fewer than 2 days total.
- The student was never late ('L') for 3 or more consecutive days.
Given an integer n, return the number of possible attendance records of length n that make a student eligible for an attendance award. The answer may be very large, so return it modulo 10^9 + 7.
Example 1:
Input: n = 2
Output: 8
Explanation: There are 8 records with length 2 that are eligible for an award:
"PP", "AP", "PA", "LP", "PL", "AL", "LA", "LL"
Only "AA" is not eligible because there are 2 absences (there need to be fewer than 2).
Example 2:
Input: n = 1
Output: 3
Example 3:
Input: n = 10101
Output: 183236316
Constraints:
- 1 <= n <= 10^5
Solution
Approach: Dynamic Programming over Six States
There are 3n records, far too many to list. But to know whether a record can still be extended, we only need two facts about it:
a: how many absences it has (0 or 1; with 2 it is already ineligible).l: how many'L'days in a row it ends with (0, 1 or 2; with 3 it is ineligible).
So build the records one day at a time and keep count[a][l], the number of eligible records of the current length in each of these 6 states. Start with the empty record: count[0][0] = 1. For each new day, every state passes its count on:
- Append
'P': goes to[a][0]. The absences stay, the late streak is broken. - Append
'A': only froma == 0, and goes to[1][0]. An absence also breaks the late streak. - Append
'L': only froml < 2, and goes to[a][l + 1].
After n days, the answer is the sum of all 6 counts. Take every sum modulo 109 + 7 as you go; in Java and C++ store the counts as 64-bit integers so that adding two of them cannot overflow.
The easy mistake is forgetting that 'A' resets the late streak: "LLALL" is eligible, so appending 'A' must move to l = 0, not keep l.
For n = 2 the states end as [0][0] = 2 (PP, LP), [0][1] = 1 (PL), [0][2] = 1 (LL), [1][0] = 3 (AP, PA, LA) and [1][1] = 1 (AL), which sums to 8.
Complexity: O(n) time and O(1) space, since each day updates a fixed 2 × 3 table.
class Solution:
def checkRecord(self, n: int) -> int:
MOD = 10**9 + 7
# count[a][l]: eligible records of the current length with `a` absences
# that end in exactly `l` consecutive 'L' days
count = [[0] * 3 for _ in range(2)]
count[0][0] = 1 # the empty record
for _ in range(n):
next_count = [[0] * 3 for _ in range(2)]
for a in range(2):
for l in range(3):
ways = count[a][l]
# 'P': same absences, the late streak is broken
next_count[a][0] = (next_count[a][0] + ways) % MOD
# 'A': allowed only if there is no absence yet; it also breaks the streak
if a == 0:
next_count[1][0] = (next_count[1][0] + ways) % MOD
# 'L': allowed only if the streak stays below 3
if l < 2:
next_count[a][l + 1] = (next_count[a][l + 1] + ways) % MOD
count = next_count
total = 0
for a in range(2):
for l in range(3):
total = (total + count[a][l]) % MOD
return total
Now write it from memory
Close the write-up and solve it yourself against the tests.