Skip to content

Commit 60eb865

Browse files
committed
Add solution and test-cases for problem 552
1 parent 09a1dcb commit 60eb865

File tree

3 files changed

+69
-34
lines changed

3 files changed

+69
-34
lines changed

leetcode/501-600/0552.Student-Attendance-Record-II/README.md

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,42 @@
22

33
## Description
44

5-
Given a positive integer n, return the number of all possible attendance records with length n, which will be regarded as rewardable. The answer may be very large, return it after mod 109 + 7.
5+
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:
66

7-
A student attendance record is a string that only contains the following three characters:
7+
- `'A'`: Absent.
8+
- `'L'`: Late.
9+
- `'P'`: Present.
810

9-
'A' : Absent.
10-
'L' : Late.
11-
'P' : Present.
12-
A record is regarded as rewardable if it doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late).
13-
**Example 1:**
11+
Any student is eligible for an attendance award if they meet **both** of the following criteria:
1412

15-
```
16-
Input: n = 2
17-
Output: 8
18-
Explanation:
19-
There are 8 records with length 2 will be regarded as rewardable:
20-
"PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL"
21-
Only "AA" won't be regarded as rewardable owing to more than one absent times.
22-
```
13+
- The student was absent (`'A'`) for **strictly** fewer than 2 days **total**.
14+
- The student was **never** late (`'L'`) for 3 or more **consecutive** days.
2315

24-
**Tags:** Math, String
16+
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`.
2517

26-
## 题意
27-
> 求2数之和
2818

29-
## 题解
19+
**Example 1:**
3020

31-
### 思路1
32-
> 。。。。
21+
```
22+
Input: n = 2
23+
Output: 8
24+
Explanation: There are 8 records with length 2 that are eligible for an award:
25+
"PP", "AP", "PA", "LP", "PL", "AL", "LA", "LL"
26+
Only "AA" is not eligible because there are 2 absences (there need to be fewer than 2).
27+
```
3328

34-
```go
29+
**Example 2:**
3530

31+
```
32+
Input: n = 1
33+
Output: 3
3634
```
3735

38-
### 思路2
39-
> 思路2
40-
```go
36+
**Example 3:**
4137

38+
```
39+
Input: n = 10101
40+
Output: 183236316
4241
```
4342

4443
## 结语
Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
const mod552 = 1000000007
4+
5+
func Solution(n int) int {
6+
// 直接dfs很明显,10101那个例子就过不去了,需要增加cache
7+
cache := make([][][]int, n+1)
8+
for i := 0; i <= n; i++ {
9+
cache[i] = make([][]int, 3)
10+
for j := 0; j < 3; j++ {
11+
cache[i][j] = make([]int, 4)
12+
for k := 0; k < 4; k++ {
13+
cache[i][j][k] = -1
14+
}
15+
}
16+
}
17+
var dfs func(int, int, int) int
18+
dfs = func(nn, a, l int) int {
19+
// a<2&&l<3
20+
if a >= 2 || l >= 3 {
21+
return 0
22+
}
23+
if nn == 0 {
24+
//放满了,
25+
return 1
26+
}
27+
if cache[nn][a][l] != -1 {
28+
return cache[nn][a][l]
29+
}
30+
ans := 0
31+
// 选择一个P,下一个随便放
32+
ans = (ans + dfs(nn-1, a, 0)) % mod552
33+
// 放置一个A
34+
ans = (ans + dfs(nn-1, a+1, 0)) % mod552
35+
// 放置一个L, L需要连续,所以放A和P的时候将L设置为0,这里是+1
36+
ans = (ans + dfs(nn-1, a, l+1)) % mod552
37+
cache[nn][a][l] = ans
38+
return ans
39+
}
40+
return dfs(n, 0, 0)
541
}

leetcode/501-600/0552.Student-Attendance-Record-II/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 2, 8},
17+
{"TestCase2", 1, 3},
18+
{"TestCase3", 10101, 183236316},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)