From 41d6eb433250347da8ddff6a2d6361722f103a0d Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 9 Sep 2025 09:17:39 +0800 Subject: [PATCH] Add solution and test-cases for problem 2327 --- .../README.md | 37 ++++++++++++------- .../Solution.go | 27 +++++++++++++- .../Solution_test.go | 21 +++++------ 3 files changed, 58 insertions(+), 27 deletions(-) diff --git a/leetcode/2301-2400/2327.Number-of-People-Aware-of-a-Secret/README.md b/leetcode/2301-2400/2327.Number-of-People-Aware-of-a-Secret/README.md index cf66c0936..18dfbdf12 100755 --- a/leetcode/2301-2400/2327.Number-of-People-Aware-of-a-Secret/README.md +++ b/leetcode/2301-2400/2327.Number-of-People-Aware-of-a-Secret/README.md @@ -1,28 +1,37 @@ # [2327.Number of People Aware of a Secret][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +On day `1`, one person discovers a secret. + +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. + +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`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 6, delay = 2, forget = 4 +Output: 5 +Explanation: +Day 1: Suppose the first person is named A. (1 person) +Day 2: A is the only person who knows the secret. (1 person) +Day 3: A shares the secret with a new person, B. (2 people) +Day 4: A shares the secret with a new person, C. (3 people) +Day 5: A forgets the secret, and B shares the secret with a new person, D. (3 people) +Day 6: B shares the secret with E, and C shares the secret with F. (5 people) ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Number of People Aware of a Secret -```go ``` - +Input: n = 4, delay = 1, forget = 3 +Output: 6 +Explanation: +Day 1: The first person is named A. (1 person) +Day 2: A shares the secret with B. (2 people) +Day 3: A and B share the secret with 2 new people, C and D. (4 people) +Day 4: A forgets the secret. B, C, and D share the secret with 3 new people. (6 people) +``` ## 结语 diff --git a/leetcode/2301-2400/2327.Number-of-People-Aware-of-a-Secret/Solution.go b/leetcode/2301-2400/2327.Number-of-People-Aware-of-a-Secret/Solution.go index d115ccf5e..a580ec249 100644 --- a/leetcode/2301-2400/2327.Number-of-People-Aware-of-a-Secret/Solution.go +++ b/leetcode/2301-2400/2327.Number-of-People-Aware-of-a-Secret/Solution.go @@ -1,5 +1,28 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(n int, delay int, forget int) int { + MOD := 1_000_000_007 + dp := make([]int, n+forget+1) + inc := make([]int, n+forget+1) + + // init + dp[1] = 1 + dp[1+forget] = -1 + + for j := 1 + delay; j < 1+forget; j++ { + inc[j] = 1 + } + + for i := 2; i <= n; i++ { + dp[i] = (dp[i] + dp[i-1] + inc[i]) % MOD + dp[i+forget] = (dp[i+forget] - inc[i]) % MOD + for j := i + delay; j < i+forget; j++ { + inc[j] = (inc[j] + inc[i]) % MOD + } + } + result := dp[n] % MOD + if result < 0 { + result += MOD + } + return result } diff --git a/leetcode/2301-2400/2327.Number-of-People-Aware-of-a-Secret/Solution_test.go b/leetcode/2301-2400/2327.Number-of-People-Aware-of-a-Secret/Solution_test.go index 14ff50eb4..b0ad3ce74 100644 --- a/leetcode/2301-2400/2327.Number-of-People-Aware-of-a-Secret/Solution_test.go +++ b/leetcode/2301-2400/2327.Number-of-People-Aware-of-a-Secret/Solution_test.go @@ -9,31 +9,30 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + n, delay, forget int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 6, 2, 4, 5}, + {"TestCase2", 4, 1, 3, 6}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.n, c.delay, c.forget) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v", + c.expect, got, c.n, c.delay, c.forget) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }