Skip to content

Commit

Permalink
Merge pull request #886 from 0xff-dev/552
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 552
  • Loading branch information
6boris committed Jun 6, 2024
2 parents ca94e3c + 60eb865 commit 59d2416
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 34 deletions.
49 changes: 24 additions & 25 deletions leetcode/501-600/0552.Student-Attendance-Record-II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,42 @@

## Description

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.
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 student attendance record is a string that only contains the following three characters:
- `'A'`: Absent.
- `'L'`: Late.
- `'P'`: Present.

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

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

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

## 题意
> 求2数之和

## 题解
**Example 1:**

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

```go
**Example 2:**

```
Input: n = 1
Output: 3
```

### 思路2
> 思路2
```go
**Example 3:**

```
Input: n = 10101
Output: 183236316
```

## 结语
Expand Down
40 changes: 38 additions & 2 deletions leetcode/501-600/0552.Student-Attendance-Record-II/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
package Solution

func Solution(x bool) bool {
return x
const mod552 = 1000000007

func Solution(n int) int {
// 直接dfs很明显,10101那个例子就过不去了,需要增加cache
cache := make([][][]int, n+1)
for i := 0; i <= n; i++ {
cache[i] = make([][]int, 3)
for j := 0; j < 3; j++ {
cache[i][j] = make([]int, 4)
for k := 0; k < 4; k++ {
cache[i][j][k] = -1
}
}
}
var dfs func(int, int, int) int
dfs = func(nn, a, l int) int {
// a<2&&l<3
if a >= 2 || l >= 3 {
return 0
}
if nn == 0 {
//放满了,
return 1
}
if cache[nn][a][l] != -1 {
return cache[nn][a][l]
}
ans := 0
// 选择一个P,下一个随便放
ans = (ans + dfs(nn-1, a, 0)) % mod552
// 放置一个A
ans = (ans + dfs(nn-1, a+1, 0)) % mod552
// 放置一个L, L需要连续,所以放A和P的时候将L设置为0,这里是+1
ans = (ans + dfs(nn-1, a, l+1)) % mod552
cache[nn][a][l] = ans
return ans
}
return dfs(n, 0, 0)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 2, 8},
{"TestCase2", 1, 3},
{"TestCase3", 10101, 183236316},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 59d2416

Please sign in to comment.