Leetcode: 552. Student Attendance Record II

Problem Statement

class Solution:
    def checkRecord(self, n: int) -> int:
        MOD = 10**9 + 7
        ans = 0

        L = 0
        P = 1
        dp = [[0, 0] for _ in range(n + 2)]

        dp[0][L] = dp[0][P] = 0
        dp[1][L] = dp[1][P] = 1
        dp[2][L] = dp[2][P] = 2

        for day in range(3, n + 1):
            dp[day][L] = (dp[day - 2][P] + dp[day - 1][P]) % MOD
            dp[day][P] = (dp[day - 1][L] + dp[day - 1][P]) % MOD

        def a(day):
            return (dp[day][L] + dp[day][P]) % MOD

        ans = a(n)
        for day in range(1, n + 1):
            ans = (ans + max(a(day - 1), 1) * max(a(n - day), 1)) % MOD
        return ans


assert Solution().checkRecord(2) == 8
assert Solution().checkRecord(1) == 3
assert Solution().checkRecord(10101) == 183236316
class Solution:
    def checkRecord(self, n: int) -> int:
        MOD = 10**9 + 7

        dp = [0] * (n + 10)
        dp[0] = 1
        dp[1] = 2
        dp[2] = 4
        dp[3] = 7

        for day in range(4, n + 1):
            dp[day] = (dp[day - 1] + dp[day - 2] + dp[day - 3]) % MOD

        ans = dp[n]
        for day in range(1, n + 1):
            ans = (ans + dp[day - 1] * dp[n - day]) % MOD
        return ans


assert Solution().checkRecord(2) == 8
assert Solution().checkRecord(1) == 3
assert Solution().checkRecord(10101) == 183236316