Skip to content

Commit 613539c

Browse files
committed
Add solution #552
1 parent 94f60bd commit 613539c

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@
437437
546|[Remove Boxes](./0546-remove-boxes.js)|Hard|
438438
547|[Number of Provinces](./0547-number-of-provinces.js)|Medium|
439439
551|[Student Attendance Record I](./0551-student-attendance-record-i.js)|Easy|
440+
552|[Student Attendance Record II](./0552-student-attendance-record-ii.js)|Hard|
440441
557|[Reverse Words in a String III](./0557-reverse-words-in-a-string-iii.js)|Easy|
441442
560|[Subarray Sum Equals K](./0560-subarray-sum-equals-k.js)|Medium|
442443
563|[Binary Tree Tilt](./0563-binary-tree-tilt.js)|Easy|
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 552. Student Attendance Record II
3+
* https://leetcode.com/problems/student-attendance-record-ii/
4+
* Difficulty: Hard
5+
*
6+
* An attendance record for a student can be represented as a string where each character
7+
* signifies whether the student was absent, late, or present on that day. The record only
8+
* contains the following three characters:
9+
* - 'A': Absent.
10+
* - 'L': Late.
11+
* - 'P': Present.
12+
*
13+
* Any student is eligible for an attendance award if they meet both of the following
14+
* criteria:
15+
* - The student was absent ('A') for strictly fewer than 2 days total.
16+
* - The student was never late ('L') for 3 or more consecutive days.
17+
*
18+
* Given an integer n, return the number of possible attendance records of length n that make
19+
* a student eligible for an attendance award. The answer may be very large, so return it
20+
* modulo 109 + 7.
21+
*/
22+
23+
/**
24+
* @param {number} n
25+
* @return {number}
26+
*/
27+
var checkRecord = function(n) {
28+
const MOD = 1e9 + 7;
29+
const dp = [[1, 0, 0], [0, 0, 0]];
30+
31+
for (let i = 0; i < n; i++) {
32+
const m = dp.map(row => [...row]);
33+
dp[0][0] = (m[0][0] + m[0][1] + m[0][2]) % MOD;
34+
dp[0][1] = m[0][0];
35+
dp[0][2] = m[0][1];
36+
dp[1][0] = (m[0][0] + m[0][1] + m[0][2] + m[1][0] + m[1][1] + m[1][2]) % MOD;
37+
dp[1][1] = m[1][0];
38+
dp[1][2] = m[1][1];
39+
}
40+
41+
return (dp[0][0] + dp[0][1] + dp[0][2] + dp[1][0] + dp[1][1] + dp[1][2]) % MOD;
42+
};

0 commit comments

Comments
 (0)