diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /student_attendance_record2.py | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'student_attendance_record2.py')
-rw-r--r-- | student_attendance_record2.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/student_attendance_record2.py b/student_attendance_record2.py new file mode 100644 index 0000000..14a37a5 --- /dev/null +++ b/student_attendance_record2.py @@ -0,0 +1,41 @@ +# 552. Student Attendance Record II + +""" +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, and 'p': present. any student is eligible for attendance award if +they meet both of the following criteria. the student was absent 'a' for +strictly fewer than 2 days total, and 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 attendance +award. return it mod 1e9+7. +""" + + +class Solution(object): + def checkRecord(self, n): + """ + :type n: int + :rtype: int + """ + if n == 1: + return 3 + if n == 0: + return 0 + nums, i = [1, 1, 2], 2 + while i < n: + nums.append((nums[i] + nums[i - 1] + nums[i - 2]) % 10**9 + 7) + i += 1 + ans = (nums[n] + nums[n - 1] + nums[n - 2]) % 10**9 + 7 + for i in range(n): + ans += nums[i + 1] * nums[n - i] * 10**9 + 7 + ans %= 10**9 + 7 + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.checkRecord(2)) + print(obj.checkRecord(1)) + print(obj.checkRecord(10101)) |