-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0552-student-attendance-record-ii.py
More file actions
34 lines (29 loc) · 1.04 KB
/
0552-student-attendance-record-ii.py
File metadata and controls
34 lines (29 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# time complexity: O(n)
# space complexity: O(n)
class Solution:
def checkRecord(self, n: int) -> int:
MOD = 1000000007
memo = [[[-1] * 3 for _ in range(2)] for _ in range(n + 1)]
def eligibleComb(n, totalAbsences, consecutiveLates):
if totalAbsences >= 2 or consecutiveLates >= 3:
return 0
if n == 0:
return 1
if memo[n][totalAbsences][consecutiveLates] != -1:
return memo[n][totalAbsences][consecutiveLates]
count = eligibleComb(n - 1, totalAbsences, 0)
count = (
count +
eligibleComb(n - 1, totalAbsences + 1, 0)
) % MOD
count = (
count +
eligibleComb(n - 1,
totalAbsences,
consecutiveLates + 1)
) % MOD
memo[n][totalAbsences][consecutiveLates] = count
return count
return eligibleComb(n, 0, 0)
n = 2
print(Solution().checkRecord(n))