Skip to content

Commit 4cde277

Browse files
authored
Merge pull request #1308 from 0xff-dev/2327
Add solution and test-cases for problem 2327
2 parents 11e2cae + 41d6eb4 commit 4cde277

File tree

3 files changed

+58
-27
lines changed

3 files changed

+58
-27
lines changed

leetcode/2301-2400/2327.Number-of-People-Aware-of-a-Secret/README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [2327.Number of People Aware of a Secret][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
On day `1`, one person discovers a secret.
5+
6+
You are given an integer `delay`, which means that each person will **share** the secret with a new person **every day**, starting from `delay` days after discovering the secret. You are also given an integer `forget`, which means that each person will **forget** the secret `forget` days after discovering it. A person **cannot** share the secret on the same day they forgot it, or on any day afterwards.
7+
8+
Given an integer `n`, return the number of people who know the secret at the end of day `n`. Since the answer may be very large, return it **modulo** `10^9 + 7`.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: n = 6, delay = 2, forget = 4
14+
Output: 5
15+
Explanation:
16+
Day 1: Suppose the first person is named A. (1 person)
17+
Day 2: A is the only person who knows the secret. (1 person)
18+
Day 3: A shares the secret with a new person, B. (2 people)
19+
Day 4: A shares the secret with a new person, C. (3 people)
20+
Day 5: A forgets the secret, and B shares the secret with a new person, D. (3 people)
21+
Day 6: B shares the secret with E, and C shares the secret with F. (5 people)
1322
```
1423

15-
## 题意
16-
> ...
24+
**Example 2:**
1725

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Number of People Aware of a Secret
23-
```go
2426
```
25-
27+
Input: n = 4, delay = 1, forget = 3
28+
Output: 6
29+
Explanation:
30+
Day 1: The first person is named A. (1 person)
31+
Day 2: A shares the secret with B. (2 people)
32+
Day 3: A and B share the secret with 2 new people, C and D. (4 people)
33+
Day 4: A forgets the secret. B, C, and D share the secret with 3 new people. (6 people)
34+
```
2635

2736
## 结语
2837

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int, delay int, forget int) int {
4+
MOD := 1_000_000_007
5+
dp := make([]int, n+forget+1)
6+
inc := make([]int, n+forget+1)
7+
8+
// init
9+
dp[1] = 1
10+
dp[1+forget] = -1
11+
12+
for j := 1 + delay; j < 1+forget; j++ {
13+
inc[j] = 1
14+
}
15+
16+
for i := 2; i <= n; i++ {
17+
dp[i] = (dp[i] + dp[i-1] + inc[i]) % MOD
18+
dp[i+forget] = (dp[i+forget] - inc[i]) % MOD
19+
for j := i + delay; j < i+forget; j++ {
20+
inc[j] = (inc[j] + inc[i]) % MOD
21+
}
22+
}
23+
result := dp[n] % MOD
24+
if result < 0 {
25+
result += MOD
26+
}
27+
return result
528
}

leetcode/2301-2400/2327.Number-of-People-Aware-of-a-Secret/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
n, delay, forget int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 6, 2, 4, 5},
17+
{"TestCase2", 4, 1, 3, 6},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.n, c.delay, c.forget)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
26+
c.expect, got, c.n, c.delay, c.forget)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)