Skip to content

Commit d483435

Browse files
committed
+ problem 2327
1 parent 66d15f4 commit d483435

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# 2327. Number of People Aware of a Secret
2+
On day `1`, one person discovers a secret.
3+
4+
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.
5+
6+
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** <code>10<sup>9</sup> + 7</code>.
7+
8+
#### Example 1:
9+
<pre>
10+
<strong>Input:</strong> n = 6, delay = 2, forget = 4
11+
<strong>Output:</strong> 5
12+
<strong>Explanation:</strong>
13+
Day 1: Suppose the first person is named A. (1 person)
14+
Day 2: A is the only person who knows the secret. (1 person)
15+
Day 3: A shares the secret with a new person, B. (2 people)
16+
Day 4: A shares the secret with a new person, C. (3 people)
17+
Day 5: A forgets the secret, and B shares the secret with a new person, D. (3 people)
18+
Day 6: B shares the secret with E, and C shares the secret with F. (5 people)
19+
</pre>
20+
21+
#### Example 2:
22+
<pre>
23+
<strong>Input:</strong> n = 4, delay = 1, forget = 3
24+
<strong>Output:</strong> 6
25+
<strong>Explanation:</strong>
26+
Day 1: The first person is named A. (1 person)
27+
Day 2: A shares the secret with B. (2 people)
28+
Day 3: A and B share the secret with 2 new people, C and D. (4 people)
29+
Day 4: A forgets the secret. B, C, and D share the secret with 3 new people. (6 people)
30+
</pre>
31+
32+
#### Constraints:
33+
* `2 <= n <= 1000`
34+
* `1 <= delay < forget <= n`
35+
36+
## Solutions (Python)
37+
38+
### 1. Solution
39+
```Python
40+
class Solution:
41+
def peopleAwareOfSecret(self, n: int, delay: int, forget: int) -> int:
42+
dp = [[0, 0, 0] for _ in range(n + 2)]
43+
dp[1 + forget][0] = 1
44+
dp[1 + delay][1] = 1
45+
dp[1][2] = 1
46+
47+
for i in range(2, n + 1):
48+
if i > 1 + forget:
49+
dp[i][0] = dp[i - forget][1]
50+
if i > 1 + delay:
51+
dp[i][1] = dp[i - 1][1] - dp[i][0] + dp[i - delay][1]
52+
dp[i][2] = dp[i - 1][2] - dp[i][0] + dp[i][1]
53+
54+
return dp[n][2] % 1000000007
55+
```
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# 2327. 知道秘密的人数
2+
在第 `1` 天,有一个人发现了一个秘密。
3+
4+
给你一个整数 `delay` ,表示每个人会在发现秘密后的 `delay` 天之后,**每天** 给一个新的人 **分享** 秘密。同时给你一个整数 `forget` ,表示每个人在发现秘密 `forget` 天之后会 **忘记** 这个秘密。一个人 **不能** 在忘记秘密那一天及之后的日子里分享秘密。
5+
6+
给你一个整数 `n` ,请你返回在第 `n` 天结束时,知道秘密的人数。由于答案可能会很大,请你将结果对 <code>10<sup>9</sup> + 7</code> **取余** 后返回。
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong> n = 6, delay = 2, forget = 4
11+
<strong>输出:</strong> 5
12+
<strong>解释:</strong>
13+
第 1 天:假设第一个人叫 A 。(一个人知道秘密)
14+
第 2 天:A 是唯一一个知道秘密的人。(一个人知道秘密)
15+
第 3 天:A 把秘密分享给 B 。(两个人知道秘密)
16+
第 4 天:A 把秘密分享给一个新的人 C 。(三个人知道秘密)
17+
第 5 天:A 忘记了秘密,B 把秘密分享给一个新的人 D 。(三个人知道秘密)
18+
第 6 天:B 把秘密分享给 E,C 把秘密分享给 F 。(五个人知道秘密)
19+
</pre>
20+
21+
#### 示例 2:
22+
<pre>
23+
<strong>输入:</strong> n = 4, delay = 1, forget = 3
24+
<strong>输出:</strong> 6
25+
<strong>解释:</strong>
26+
第 1 天:第一个知道秘密的人为 A 。(一个人知道秘密)
27+
第 2 天:A 把秘密分享给 B 。(两个人知道秘密)
28+
第 3 天:A 和 B 把秘密分享给 2 个新的人 C 和 D 。(四个人知道秘密)
29+
第 4 天:A 忘记了秘密,B、C、D 分别分享给 3 个新的人。(六个人知道秘密)
30+
</pre>
31+
32+
#### 提示:
33+
* `2 <= n <= 1000`
34+
* `1 <= delay < forget <= n`
35+
36+
## 题解 (Python)
37+
38+
### 1. 题解
39+
```Python
40+
class Solution:
41+
def peopleAwareOfSecret(self, n: int, delay: int, forget: int) -> int:
42+
dp = [[0, 0, 0] for _ in range(n + 2)]
43+
dp[1 + forget][0] = 1
44+
dp[1 + delay][1] = 1
45+
dp[1][2] = 1
46+
47+
for i in range(2, n + 1):
48+
if i > 1 + forget:
49+
dp[i][0] = dp[i - forget][1]
50+
if i > 1 + delay:
51+
dp[i][1] = dp[i - 1][1] - dp[i][0] + dp[i - delay][1]
52+
dp[i][2] = dp[i - 1][2] - dp[i][0] + dp[i][1]
53+
54+
return dp[n][2] % 1000000007
55+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def peopleAwareOfSecret(self, n: int, delay: int, forget: int) -> int:
3+
dp = [[0, 0, 0] for _ in range(n + 2)]
4+
dp[1 + forget][0] = 1
5+
dp[1 + delay][1] = 1
6+
dp[1][2] = 1
7+
8+
for i in range(2, n + 1):
9+
if i > 1 + forget:
10+
dp[i][0] = dp[i - forget][1]
11+
if i > 1 + delay:
12+
dp[i][1] = dp[i - 1][1] - dp[i][0] + dp[i - delay][1]
13+
dp[i][2] = dp[i - 1][2] - dp[i][0] + dp[i][1]
14+
15+
return dp[n][2] % 1000000007

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,7 @@
11731173
[2320][2320l]|[Count Number of Ways to Place Houses][2320] |![rs]
11741174
[2325][2325l]|[Decode the Message][2325] |![py]
11751175
[2326][2326l]|[Spiral Matrix IV][2326] |![py]
1176+
[2327][2327l]|[Number of People Aware of a Secret][2327] |![py]
11761177
[2331][2331l]|[Evaluate Boolean Binary Tree][2331] |![py]
11771178
[2335][2335l]|[Minimum Amount of Time to Fill Cups][2335] |![rs]
11781179
[2336][2336l]|[Smallest Number in Infinite Set][2336] |![rs]
@@ -2466,6 +2467,7 @@
24662467
[2320]:Problemset/2320-Count%20Number%20of%20Ways%20to%20Place%20Houses/README.md#2320-count-number-of-ways-to-place-houses
24672468
[2325]:Problemset/2325-Decode%20the%20Message/README.md#2325-decode-the-message
24682469
[2326]:Problemset/2326-Spiral%20Matrix%20IV/README.md#2326-spiral-matrix-iv
2470+
[2327]:Problemset/2327-Number%20of%20People%20Aware%20of%20a%20Secret/README.md#2327-number-of-people-aware-of-a-secret
24692471
[2331]:Problemset/2331-Evaluate%20Boolean%20Binary%20Tree/README.md#2331-evaluate-boolean-binary-tree
24702472
[2335]:Problemset/2335-Minimum%20Amount%20of%20Time%20to%20Fill%20Cups/README.md#2335-minimum-amount-of-time-to-fill-cups
24712473
[2336]:Problemset/2336-Smallest%20Number%20in%20Infinite%20Set/README.md#2336-smallest-number-in-infinite-set
@@ -3762,6 +3764,7 @@
37623764
[2320l]:https://leetcode.com/problems/count-number-of-ways-to-place-houses/
37633765
[2325l]:https://leetcode.com/problems/decode-the-message/
37643766
[2326l]:https://leetcode.com/problems/spiral-matrix-iv/
3767+
[2327l]:https://leetcode.com/problems/number-of-people-aware-of-a-secret/
37653768
[2331l]:https://leetcode.com/problems/evaluate-boolean-binary-tree/
37663769
[2335l]:https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups/
37673770
[2336l]:https://leetcode.com/problems/smallest-number-in-infinite-set/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,7 @@
11731173
[2320][2320l]|[统计放置房子的方式数][2320] |![rs]
11741174
[2325][2325l]|[解密消息][2325] |![py]
11751175
[2326][2326l]|[螺旋矩阵 IV][2326] |![py]
1176+
[2327][2327l]|[知道秘密的人数][2327] |![py]
11761177
[2331][2331l]|[计算布尔二叉树的值][2331] |![py]
11771178
[2335][2335l]|[装满杯子需要的最短总时长][2335] |![rs]
11781179
[2336][2336l]|[无限集中的最小数字][2336] |![rs]
@@ -2466,6 +2467,7 @@
24662467
[2320]:Problemset/2320-Count%20Number%20of%20Ways%20to%20Place%20Houses/README_CN.md#2320-统计放置房子的方式数
24672468
[2325]:Problemset/2325-Decode%20the%20Message/README_CN.md#2325-解密消息
24682469
[2326]:Problemset/2326-Spiral%20Matrix%20IV/README_CN.md#2326-螺旋矩阵-iv
2470+
[2327]:Problemset/2327-Number%20of%20People%20Aware%20of%20a%20Secret/README_CN.md#2327-知道秘密的人数
24692471
[2331]:Problemset/2331-Evaluate%20Boolean%20Binary%20Tree/README_CN.md#2331-计算布尔二叉树的值
24702472
[2335]:Problemset/2335-Minimum%20Amount%20of%20Time%20to%20Fill%20Cups/README_CN.md#2335-装满杯子需要的最短总时长
24712473
[2336]:Problemset/2336-Smallest%20Number%20in%20Infinite%20Set/README_CN.md#2336-无限集中的最小数字
@@ -3762,6 +3764,7 @@
37623764
[2320l]:https://leetcode.cn/problems/count-number-of-ways-to-place-houses/
37633765
[2325l]:https://leetcode.cn/problems/decode-the-message/
37643766
[2326l]:https://leetcode.cn/problems/spiral-matrix-iv/
3767+
[2327l]:https://leetcode.cn/problems/number-of-people-aware-of-a-secret/
37653768
[2331l]:https://leetcode.cn/problems/evaluate-boolean-binary-tree/
37663769
[2335l]:https://leetcode.cn/problems/minimum-amount-of-time-to-fill-cups/
37673770
[2336l]:https://leetcode.cn/problems/smallest-number-in-infinite-set/

0 commit comments

Comments
 (0)