Skip to content

Commit c7e5241

Browse files
authored
update: 添加问题“2327.知道秘密的人数”的代码和题解 (#1122)
* 2327: AC.cpp (#1121) cpp - AC,36.69%,23.02% * word: 今早CV的昨天题的解法二模板到现在也还没来得及写 * 2327: WA.cpp (#1121) * 2327: WA.cpp (#1121) * 2327: WA.cpp (#1121) - 果然要+mod再模(至少最后) * 2327: AC.cpp (#1121) cpp - AC,100.00%, 64.29% * update: 添加问题“2327.知道秘密的人数”的代码和题解 (#1122) Signed-off-by: LetMeFly666 <[email protected]> * docs: multi 2327 (#1121) #1122 (comment) --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent 7c105f5 commit c7e5241

File tree

5 files changed

+239
-0
lines changed

5 files changed

+239
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-09-09 23:42:14
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-09-09 23:52:39
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
using ll = long long;
12+
const ll MOD = 1e9 + 7;
13+
class Solution {
14+
public:
15+
int peopleAwareOfSecret(int n, int delay, int forget) {
16+
vector<ll> dp(n);
17+
dp[0] = 1;
18+
for (int i = 0; i < n; i++) {
19+
for (int j = i + delay; j < i + forget && j < n; j++) {
20+
dp[j] = (dp[j] + dp[i]) % MOD;
21+
}
22+
}
23+
ll ans = 0;
24+
for (int i = 0; i < forget; i++) {
25+
ans = (ans + dp[n - i - 1]) % MOD;
26+
}
27+
return ans;
28+
}
29+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-09-09 23:42:14
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-09-11 10:49:54
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
using ll = long long;
12+
const ll MOD = 1e9 + 7;
13+
class Solution {
14+
public:
15+
int peopleAwareOfSecret(int n, int delay, int forget) {
16+
vector<ll> diff(n + 1);
17+
diff[1] = 1;
18+
diff[2] = -1;
19+
ll now = 0, ans = 0;
20+
for (int i = 1; i <= n; i++) {
21+
now = (now + diff[i]) % MOD;
22+
if (i + forget > n) {
23+
ans = (ans + now) % MOD;
24+
}
25+
if (i + delay <= n) {
26+
diff[i + delay] = (diff[i + delay] + now) % MOD;
27+
}
28+
if (i + forget <= n) {
29+
diff[i + forget] = (diff[i + forget] + MOD - now) % MOD;
30+
}
31+
}
32+
return ans;
33+
}
34+
};
35+
36+
#if defined(_WIN32) || defined(__APPLE__)
37+
/*
38+
6 2 4
39+
40+
5
41+
*/
42+
int main() {
43+
int a, b, c;
44+
while (cin >> a >> b >> c) {
45+
Solution sol;
46+
cout << sol.peopleAwareOfSecret(a, b, c) << endl;
47+
}
48+
return 0;
49+
}
50+
#endif

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@
740740
|2311.小于等于K的最长二进制子序列|中等|<a href="https://leetcode.cn/problems/longest-binary-subsequence-less-than-or-equal-to-k/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/06/26/LeetCode%202311.%E5%B0%8F%E4%BA%8E%E7%AD%89%E4%BA%8EK%E7%9A%84%E6%9C%80%E9%95%BF%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%90%E5%BA%8F%E5%88%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148935308" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/longest-binary-subsequence-less-than-or-equal-to-k/solutions/3709306/letmefly-2311xiao-yu-deng-yu-k-de-zui-ch-2n27/" target="_blank">LeetCode题解</a>|
741741
|2312.卖木头块|困难|<a href="https://leetcode.cn/problems/selling-pieces-of-wood/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/15/LeetCode%202312.%E5%8D%96%E6%9C%A8%E5%A4%B4%E5%9D%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136747100" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/selling-pieces-of-wood/solutions/2689067/letmefly-2312mai-mu-tou-kuai-dong-tai-gu-vk5a/" target="_blank">LeetCode题解</a>|
742742
|2316.统计无向图中无法互相到达点对数|中等|<a href="https://leetcode.cn/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/10/21/LeetCode%202316.%E7%BB%9F%E8%AE%A1%E6%97%A0%E5%90%91%E5%9B%BE%E4%B8%AD%E6%97%A0%E6%B3%95%E4%BA%92%E7%9B%B8%E5%88%B0%E8%BE%BE%E7%82%B9%E5%AF%B9%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133962709" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/solutions/2492186/letmefly-2316tong-ji-wu-xiang-tu-zhong-w-79vz/" target="_blank">LeetCode题解</a>|
743+
|2327.知道秘密的人数|中等|<a href="https://leetcode.cn/problems/number-of-people-aware-of-a-secret/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/09/11/LeetCode%202327.%E7%9F%A5%E9%81%93%E7%A7%98%E5%AF%86%E7%9A%84%E4%BA%BA%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151586337" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-people-aware-of-a-secret/solutions/3779067/letmefly-2327zhi-dao-mi-mi-de-ren-shu-do-y3ey/" target="_blank">LeetCode题解</a>|
743744
|2335.装满杯子需要的最短总时长|简单|<a href="https://leetcode.cn/problems/minimum-amount-of-time-to-fill-cups/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/02/11/LeetCode%202335.%E8%A3%85%E6%BB%A1%E6%9D%AF%E5%AD%90%E9%9C%80%E8%A6%81%E7%9A%84%E6%9C%80%E7%9F%AD%E6%80%BB%E6%97%B6%E9%95%BF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128980819" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-amount-of-time-to-fill-cups/solutions/2105156/letmefly-2335zhuang-man-bei-zi-xu-yao-de-kq65/" target="_blank">LeetCode题解</a>|
744745
|2336.无限集中的最小数字|中等|<a href="https://leetcode.cn/problems/smallest-number-in-infinite-set/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/11/29/LeetCode%202336.%E6%97%A0%E9%99%90%E9%9B%86%E4%B8%AD%E7%9A%84%E6%9C%80%E5%B0%8F%E6%95%B0%E5%AD%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134687046" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/smallest-number-in-infinite-set/solutions/2546486/letmefly-2336wu-xian-ji-zhong-de-zui-xia-wzz1/" target="_blank">LeetCode题解</a>|
745746
|2337.移动片段得到字符串|中等|<a href="https://leetcode.cn/problems/move-pieces-to-obtain-a-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/08/22/LeetCode%202337.%E7%A7%BB%E5%8A%A8%E7%89%87%E6%AE%B5%E5%BE%97%E5%88%B0%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/132421605" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/move-pieces-to-obtain-a-string/solutions/2399221/letmefly-2337yi-dong-pian-duan-de-dao-zi-oi31/" target="_blank">LeetCode题解</a>|
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
---
2+
title: 2327.知道秘密的人数:动态规划/差分数组O(n)
3+
date: 2025-09-11 13:00:17
4+
tags: [题解, LeetCode, 中等, 队列, 动态规划, 模拟]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】2327.知道秘密的人数:动态规划/差分数组O(n)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/number-of-people-aware-of-a-secret/](https://leetcode.cn/problems/number-of-people-aware-of-a-secret/)
11+
12+
<p>在第 <code>1</code>&nbsp;天,有一个人发现了一个秘密。</p>
13+
14+
<p>给你一个整数&nbsp;<code>delay</code>&nbsp;,表示每个人会在发现秘密后的 <code>delay</code>&nbsp;天之后,<strong>每天</strong>&nbsp;给一个新的人&nbsp;<strong>分享</strong>&nbsp;秘密。同时给你一个整数&nbsp;<code>forget</code>&nbsp;,表示每个人在发现秘密&nbsp;<code>forget</code>&nbsp;天之后会&nbsp;<strong>忘记</strong>&nbsp;这个秘密。一个人&nbsp;<strong>不能</strong>&nbsp;在忘记秘密那一天及之后的日子里分享秘密。</p>
15+
16+
<p>给你一个整数&nbsp;<code>n</code>&nbsp;,请你返回在第 <code>n</code>&nbsp;天结束时,知道秘密的人数。由于答案可能会很大,请你将结果对&nbsp;<code>10<sup>9</sup> + 7</code>&nbsp;<strong>取余</strong>&nbsp;后返回。</p>
17+
18+
<p>&nbsp;</p>
19+
20+
<p><strong>示例 1:</strong></p>
21+
22+
<pre><b>输入:</b>n = 6, delay = 2, forget = 4
23+
<b>输出:</b>5
24+
<strong>解释:</strong>
25+
第 1 天:假设第一个人叫 A 。(一个人知道秘密)
26+
第 2 天:A 是唯一一个知道秘密的人。(一个人知道秘密)
27+
第 3 天:A 把秘密分享给 B 。(两个人知道秘密)
28+
第 4 天:A 把秘密分享给一个新的人 C 。(三个人知道秘密)
29+
第 5 天:A 忘记了秘密,B 把秘密分享给一个新的人 D 。(三个人知道秘密)
30+
第 6 天:B 把秘密分享给 E,C 把秘密分享给 F 。(五个人知道秘密)
31+
</pre>
32+
33+
<p><strong>示例 2:</strong></p>
34+
35+
<pre><b>输入:</b>n = 4, delay = 1, forget = 3
36+
<b>输出:</b>6
37+
<strong>解释:</strong>
38+
第 1 天:第一个知道秘密的人为 A 。(一个人知道秘密)
39+
第 2 天:A 把秘密分享给 B 。(两个人知道秘密)
40+
第 3 天:A 和 B 把秘密分享给 2 个新的人 C 和 D 。(四个人知道秘密)
41+
第 4 天:A 忘记了秘密,B、C、D 分别分享给 3 个新的人。(六个人知道秘密)
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
46+
<p><strong>提示:</strong></p>
47+
48+
<ul>
49+
<li><code>2 &lt;= n &lt;= 1000</code></li>
50+
<li><code>1 &lt;= delay &lt; forget &lt;= n</code></li>
51+
</ul>
52+
53+
54+
55+
## 解题方法一:动态规划
56+
57+
> (为了方便描述)也可以把这道题看成细菌繁殖,细菌delay天成熟forget天死亡并且期间每天复制自身一份。
58+
59+
`dp[i]`代表第`i-1`天新生细菌的数量,初始值`dp[0]=1`,其余`dp[i]=0`
60+
61+
从第`1`天开始遍历到第`n`天,第`i`天出生的细菌可以在第`[i+delay, i+forget)`天分别产下一个新细菌,所以有`dp[i+j] += dp[i]`,其中`i+delay<=j<i+forget`
62+
63+
+ 时间复杂度$O(n\times(forget-delay))$
64+
+ 空间复杂度$O(n)$
65+
66+
### AC代码
67+
68+
#### C++
69+
70+
```cpp
71+
/*
72+
* @Author: LetMeFly
73+
* @Date: 2025-09-09 23:42:14
74+
* @LastEditors: LetMeFly.xyz
75+
* @LastEditTime: 2025-09-09 23:52:39
76+
*/
77+
#if defined(_WIN32) || defined(__APPLE__)
78+
#include "_[1,2]toVector.h"
79+
#endif
80+
81+
using ll = long long;
82+
const ll MOD = 1e9 + 7;
83+
class Solution {
84+
public:
85+
int peopleAwareOfSecret(int n, int delay, int forget) {
86+
vector<ll> dp(n);
87+
dp[0] = 1;
88+
for (int i = 0; i < n; i++) {
89+
for (int j = i + delay; j < i + forget && j < n; j++) {
90+
dp[j] = (dp[j] + dp[i]) % MOD;
91+
}
92+
}
93+
ll ans = 0;
94+
for (int i = 0; i < forget; i++) {
95+
ans = (ans + dp[n - i - 1]) % MOD;
96+
}
97+
return ans;
98+
}
99+
};
100+
```
101+
102+
## 解题方法二:查分数组
103+
104+
方法一中有一个耗时操作:对于第`i`天出生的细菌,令`i+delay`到`i+forget-1`天的细菌分别加上`dp[i]`。
105+
106+
这个耗时操作不正是差分数组可以优化的吗?
107+
108+
令`diff[i]=dp[i]-dp[i-1]`,想让`dp[i+delay]`到`dp[i+forget-1]`每个加`dp[i]`只需要令`diff[i+delay]+=dp[i]`和`diff[i+forget]-=dp[i]`。
109+
110+
相应的,`dp[i]`就等于`sum(diff[0..i+1])`。
111+
112+
+ 时间复杂度$O(n)$
113+
+ 空间复杂度$O(n)$
114+
115+
### AC代码
116+
117+
#### C++
118+
119+
```cpp
120+
/*
121+
* @Author: LetMeFly
122+
* @Date: 2025-09-09 23:42:14
123+
* @LastEditors: LetMeFly.xyz
124+
* @LastEditTime: 2025-09-11 10:49:54
125+
*/
126+
using ll = long long;
127+
const ll MOD = 1e9 + 7;
128+
class Solution {
129+
public:
130+
int peopleAwareOfSecret(int n, int delay, int forget) {
131+
vector<ll> diff(n + 1);
132+
diff[1] = 1;
133+
diff[2] = -1;
134+
ll now = 0, ans = 0;
135+
for (int i = 1; i <= n; i++) {
136+
now = (now + diff[i]) % MOD;
137+
if (i + forget > n) {
138+
ans = (ans + now) % MOD;
139+
}
140+
if (i + delay <= n) {
141+
diff[i + delay] = (diff[i + delay] + now) % MOD;
142+
}
143+
if (i + forget <= n) {
144+
diff[i + forget] = (diff[i + forget] + MOD - now) % MOD;
145+
}
146+
}
147+
return ans;
148+
}
149+
};
150+
```
151+
152+
153+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/151586337)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/09/11/LeetCode%202327.%E7%9F%A5%E9%81%93%E7%A7%98%E5%AF%86%E7%9A%84%E4%BA%BA%E6%95%B0/)哦~
154+
>
155+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,10 @@ categories: [自用]
14231423
|induction|n. 就职(仪式),入门,诱导,归纳法,电磁效应|
14241424
|||
14251425
|abreast|adv. 并排地<br/>adj.(少见) 并肩的|
1426+
|||
1427+
|diction|n. 用词,用语,措辞,吐字|
1428+
|||
1429+
|faction|n. (大团体中的)派系,小集团,派系斗争,内讧|
14261430

14271431
+ 这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
14281432
+ 也许可以加个AI用最近词编故事功能(返回接口中支持标注所使用单词高亮?)

0 commit comments

Comments
 (0)