Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6e4118a

Browse files
feat: add solutions to lc problem: No.2327 (#4707)
1 parent 368460e commit 6e4118a

File tree

6 files changed

+237
-115
lines changed

6 files changed

+237
-115
lines changed

‎solution/2300-2399/2327.Number of People Aware of a Secret/README.md

Lines changed: 80 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ tags:
6969

7070
### 方法一:差分数组
7171

72-
差分数组 $d[i]$ 记录每天知道秘密的人数变化情况,$cnt[i]$ 记录第 $i$ 天新得知秘密的人数。那么从 $[i+delay,i+forget)$ 的这段时间内,$cnt[i]$ 个人每天都能分享给另外 $cnt[i]$ 个人
72+
我们用一个差分数组 $d[i]$ 来记录第 $i$ 天知道秘密的人数变化情况,用一个数组 $cnt[i]$ 来记录第 $i$ 天新得知秘密的人数
7373

74-
最终 $sum(d[:n+1])$ 就是答案
74+
那么,对于第 $i$ 天新得知秘密的 $cnt[i]$ 个人来说,他们会在 $[i+\text{delay}, i+\text{forget})$ 这段时间内每天都能分享给另外 $cnt[i]$ 个人
7575

76-
时间复杂度 $O(n^2)$。
76+
答案为 $\sum_{i=1}^{n} d[i]$。
77+
78+
时间复杂度 $O(n^2),ドル空间复杂度 $O(n)$。其中 $n$ 为题目给定的整数。
7779

7880
<!-- tabs:start -->
7981

@@ -102,27 +104,26 @@ class Solution:
102104

103105
```java
104106
class Solution {
105-
private static final int MOD = (int) 1e9 + 7;
106-
107107
public int peopleAwareOfSecret(int n, int delay, int forget) {
108+
final int mod = (int) 1e9 + 7;
108109
int m = (n << 1) + 10;
109110
long[] d = new long[m];
110111
long[] cnt = new long[m];
111112
cnt[1] = 1;
112113
for (int i = 1; i <= n; ++i) {
113114
if (cnt[i] > 0) {
114-
d[i] = (d[i] + cnt[i]) % MOD;
115-
d[i + forget] = (d[i + forget] - cnt[i] + MOD) % MOD;
115+
d[i] = (d[i] + cnt[i]) % mod;
116+
d[i + forget] = (d[i + forget] - cnt[i] + mod) % mod;
116117
int nxt = i + delay;
117118
while (nxt < i + forget) {
118-
cnt[nxt] = (cnt[nxt] + cnt[i]) % MOD;
119+
cnt[nxt] = (cnt[nxt] + cnt[i]) % mod;
119120
++nxt;
120121
}
121122
}
122123
}
123124
long ans = 0;
124125
for (int i = 1; i <= n; ++i) {
125-
ans = (ans + d[i]) % MOD;
126+
ans = (ans + d[i]) % mod;
126127
}
127128
return (int) ans;
128129
}
@@ -132,29 +133,29 @@ class Solution {
132133
#### C++
133134

134135
```cpp
135-
using ll = long long;
136-
const int mod = 1e9 + 7;
137-
138136
class Solution {
139137
public:
140138
int peopleAwareOfSecret(int n, int delay, int forget) {
139+
const int mod = 1e9 + 7;
141140
int m = (n << 1) + 10;
142-
vector<ll> d(m);
143-
vector<ll> cnt(m);
141+
vector<long long> d(m), cnt(m);
144142
cnt[1] = 1;
145-
for (int i = 1; i <= n; ++i) {
146-
if (!cnt[i]) continue;
147-
d[i] = (d[i] + cnt[i]) % mod;
148-
d[i + forget] = (d[i + forget] - cnt[i] + mod) % mod;
149-
int nxt = i + delay;
150-
while (nxt < i + forget) {
151-
cnt[nxt] = (cnt[nxt] + cnt[i]) % mod;
152-
++nxt;
143+
for (int i = 1; i <= n; i++) {
144+
if (cnt[i]) {
145+
d[i] = (d[i] + cnt[i]) % mod;
146+
d[i + forget] = (d[i + forget] - cnt[i] + mod) % mod;
147+
int nxt = i + delay;
148+
while (nxt < i + forget) {
149+
cnt[nxt] = (cnt[nxt] + cnt[i]) % mod;
150+
nxt++;
151+
}
153152
}
154153
}
155-
int ans = 0;
156-
for (int i = 1; i <= n; ++i) ans = (ans + d[i]) % mod;
157-
return ans;
154+
long long ans = 0;
155+
for (int i = 0; i <= n; i++) {
156+
ans += d[i];
157+
}
158+
return ans % mod;
158159
}
159160
};
160161
```
@@ -192,25 +193,65 @@ func peopleAwareOfSecret(n int, delay int, forget int) int {
192193

193194
```ts
194195
function peopleAwareOfSecret(n: number, delay: number, forget: number): number {
195-
let dp = new Array(n + 1).fill(0n);
196-
dp[1] = 1n;
197-
for (let i = 2; i <= n; i++) {
198-
let pre = 0n;
199-
for (let j = i - forget + 1; j <= i - delay; j++) {
200-
if (j > 0) {
201-
pre += dp[j];
196+
const mod = 1e9 + 7;
197+
const m = (n << 1) + 10;
198+
const d: number[] = Array(m).fill(0);
199+
const cnt: number[] = Array(m).fill(0);
200+
201+
cnt[1] = 1;
202+
for (let i = 1; i <= n; ++i) {
203+
if (cnt[i] > 0) {
204+
d[i] = (d[i] + cnt[i]) % mod;
205+
d[i + forget] = (d[i + forget] - cnt[i] + mod) % mod;
206+
let nxt = i + delay;
207+
while (nxt < i + forget) {
208+
cnt[nxt] = (cnt[nxt] + cnt[i]) % mod;
209+
++nxt;
202210
}
203211
}
204-
dp[i] = pre;
205212
}
206-
let pre = 0n;
207-
let i = n + 1;
208-
for (let j = i - forget; j < i; j++) {
209-
if (j > 0) {
210-
pre += dp[j];
213+
214+
let ans = 0;
215+
for (let i = 1; i <= n; ++i) {
216+
ans = (ans + d[i]) % mod;
217+
}
218+
return ans;
219+
}
220+
```
221+
222+
#### Rust
223+
224+
```rust
225+
impl Solution {
226+
pub fn people_aware_of_secret(n: i32, delay: i32, forget: i32) -> i32 {
227+
let n = n as usize;
228+
let delay = delay as usize;
229+
let forget = forget as usize;
230+
let m = (n << 1) + 10;
231+
let modulo: i64 = 1_000_000_007;
232+
233+
let mut d = vec![0i64; m];
234+
let mut cnt = vec![0i64; m];
235+
236+
cnt[1] = 1;
237+
for i in 1..=n {
238+
if cnt[i] > 0 {
239+
d[i] = (d[i] + cnt[i]) % modulo;
240+
d[i + forget] = (d[i + forget] - cnt[i] + modulo) % modulo;
241+
let mut nxt = i + delay;
242+
while nxt < i + forget {
243+
cnt[nxt] = (cnt[nxt] + cnt[i]) % modulo;
244+
nxt += 1;
245+
}
246+
}
247+
}
248+
249+
let mut ans: i64 = 0;
250+
for i in 1..=n {
251+
ans = (ans + d[i]) % modulo;
211252
}
253+
ans as i32
212254
}
213-
return Number(pre % BigInt(10 ** 9 + 7));
214255
}
215256
```
216257

‎solution/2300-2399/2327.Number of People Aware of a Secret/README_EN.md

Lines changed: 84 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ Day 4: A forgets the secret. B, C, and D share the secret with 3 new people. (6
6767

6868
<!-- solution:start -->
6969

70-
### Solution 1
70+
### Solution 1: Difference Array
71+
72+
We use a difference array $d[i]$ to record the change in the number of people who know the secret on day $i,ドル and an array $cnt[i]$ to record the number of people who newly learn the secret on day $i$.
73+
74+
For the $cnt[i]$ people who newly learn the secret on day $i,ドル they can share the secret with another $cnt[i]$ people each day during the interval $[i+\text{delay}, i+\text{forget})$.
75+
76+
The answer is $\sum_{i=1}^{n} d[i]$.
77+
78+
The time complexity is $O(n^2),ドル and the space complexity is $O(n),ドル where $n$ is the given integer.
7179

7280
<!-- tabs:start -->
7381

@@ -96,27 +104,26 @@ class Solution:
96104

97105
```java
98106
class Solution {
99-
private static final int MOD = (int) 1e9 + 7;
100-
101107
public int peopleAwareOfSecret(int n, int delay, int forget) {
108+
final int mod = (int) 1e9 + 7;
102109
int m = (n << 1) + 10;
103110
long[] d = new long[m];
104111
long[] cnt = new long[m];
105112
cnt[1] = 1;
106113
for (int i = 1; i <= n; ++i) {
107114
if (cnt[i] > 0) {
108-
d[i] = (d[i] + cnt[i]) % MOD;
109-
d[i + forget] = (d[i + forget] - cnt[i] + MOD) % MOD;
115+
d[i] = (d[i] + cnt[i]) % mod;
116+
d[i + forget] = (d[i + forget] - cnt[i] + mod) % mod;
110117
int nxt = i + delay;
111118
while (nxt < i + forget) {
112-
cnt[nxt] = (cnt[nxt] + cnt[i]) % MOD;
119+
cnt[nxt] = (cnt[nxt] + cnt[i]) % mod;
113120
++nxt;
114121
}
115122
}
116123
}
117124
long ans = 0;
118125
for (int i = 1; i <= n; ++i) {
119-
ans = (ans + d[i]) % MOD;
126+
ans = (ans + d[i]) % mod;
120127
}
121128
return (int) ans;
122129
}
@@ -126,29 +133,29 @@ class Solution {
126133
#### C++
127134

128135
```cpp
129-
using ll = long long;
130-
const int mod = 1e9 + 7;
131-
132136
class Solution {
133137
public:
134138
int peopleAwareOfSecret(int n, int delay, int forget) {
139+
const int mod = 1e9 + 7;
135140
int m = (n << 1) + 10;
136-
vector<ll> d(m);
137-
vector<ll> cnt(m);
141+
vector<long long> d(m), cnt(m);
138142
cnt[1] = 1;
139-
for (int i = 1; i <= n; ++i) {
140-
if (!cnt[i]) continue;
141-
d[i] = (d[i] + cnt[i]) % mod;
142-
d[i + forget] = (d[i + forget] - cnt[i] + mod) % mod;
143-
int nxt = i + delay;
144-
while (nxt < i + forget) {
145-
cnt[nxt] = (cnt[nxt] + cnt[i]) % mod;
146-
++nxt;
143+
for (int i = 1; i <= n; i++) {
144+
if (cnt[i]) {
145+
d[i] = (d[i] + cnt[i]) % mod;
146+
d[i + forget] = (d[i + forget] - cnt[i] + mod) % mod;
147+
int nxt = i + delay;
148+
while (nxt < i + forget) {
149+
cnt[nxt] = (cnt[nxt] + cnt[i]) % mod;
150+
nxt++;
151+
}
147152
}
148153
}
149-
int ans = 0;
150-
for (int i = 1; i <= n; ++i) ans = (ans + d[i]) % mod;
151-
return ans;
154+
long long ans = 0;
155+
for (int i = 0; i <= n; i++) {
156+
ans += d[i];
157+
}
158+
return ans % mod;
152159
}
153160
};
154161
```
@@ -186,25 +193,65 @@ func peopleAwareOfSecret(n int, delay int, forget int) int {
186193

187194
```ts
188195
function peopleAwareOfSecret(n: number, delay: number, forget: number): number {
189-
let dp = new Array(n + 1).fill(0n);
190-
dp[1] = 1n;
191-
for (let i = 2; i <= n; i++) {
192-
let pre = 0n;
193-
for (let j = i - forget + 1; j <= i - delay; j++) {
194-
if (j > 0) {
195-
pre += dp[j];
196+
const mod = 1e9 + 7;
197+
const m = (n << 1) + 10;
198+
const d: number[] = Array(m).fill(0);
199+
const cnt: number[] = Array(m).fill(0);
200+
201+
cnt[1] = 1;
202+
for (let i = 1; i <= n; ++i) {
203+
if (cnt[i] > 0) {
204+
d[i] = (d[i] + cnt[i]) % mod;
205+
d[i + forget] = (d[i + forget] - cnt[i] + mod) % mod;
206+
let nxt = i + delay;
207+
while (nxt < i + forget) {
208+
cnt[nxt] = (cnt[nxt] + cnt[i]) % mod;
209+
++nxt;
196210
}
197211
}
198-
dp[i] = pre;
199212
}
200-
let pre = 0n;
201-
let i = n + 1;
202-
for (let j = i - forget; j < i; j++) {
203-
if (j > 0) {
204-
pre += dp[j];
213+
214+
let ans = 0;
215+
for (let i = 1; i <= n; ++i) {
216+
ans = (ans + d[i]) % mod;
217+
}
218+
return ans;
219+
}
220+
```
221+
222+
#### Rust
223+
224+
```rust
225+
impl Solution {
226+
pub fn people_aware_of_secret(n: i32, delay: i32, forget: i32) -> i32 {
227+
let n = n as usize;
228+
let delay = delay as usize;
229+
let forget = forget as usize;
230+
let m = (n << 1) + 10;
231+
let modulo: i64 = 1_000_000_007;
232+
233+
let mut d = vec![0i64; m];
234+
let mut cnt = vec![0i64; m];
235+
236+
cnt[1] = 1;
237+
for i in 1..=n {
238+
if cnt[i] > 0 {
239+
d[i] = (d[i] + cnt[i]) % modulo;
240+
d[i + forget] = (d[i + forget] - cnt[i] + modulo) % modulo;
241+
let mut nxt = i + delay;
242+
while nxt < i + forget {
243+
cnt[nxt] = (cnt[nxt] + cnt[i]) % modulo;
244+
nxt += 1;
245+
}
246+
}
247+
}
248+
249+
let mut ans: i64 = 0;
250+
for i in 1..=n {
251+
ans = (ans + d[i]) % modulo;
205252
}
253+
ans as i32
206254
}
207-
return Number(pre % BigInt(10 ** 9 + 7));
208255
}
209256
```
210257

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
using ll = long long;
2-
const int mod = 1e9 + 7;
3-
41
class Solution {
52
public:
63
int peopleAwareOfSecret(int n, int delay, int forget) {
4+
const int mod = 1e9 + 7;
75
int m = (n << 1) + 10;
8-
vector<ll> d(m);
9-
vector<ll> cnt(m);
6+
vector<long long> d(m), cnt(m);
107
cnt[1] = 1;
11-
for (int i = 1; i <= n; ++i) {
12-
if (!cnt[i]) continue;
13-
d[i] = (d[i] + cnt[i]) % mod;
14-
d[i + forget] = (d[i + forget] - cnt[i] + mod) % mod;
15-
int nxt = i + delay;
16-
while (nxt < i + forget) {
17-
cnt[nxt] = (cnt[nxt] + cnt[i]) % mod;
18-
++nxt;
8+
for (int i = 1; i <= n; i++) {
9+
if (cnt[i]) {
10+
d[i] = (d[i] + cnt[i]) % mod;
11+
d[i + forget] = (d[i + forget] - cnt[i] + mod) % mod;
12+
int nxt = i + delay;
13+
while (nxt < i + forget) {
14+
cnt[nxt] = (cnt[nxt] + cnt[i]) % mod;
15+
nxt++;
16+
}
1917
}
2018
}
21-
int ans = 0;
22-
for (int i = 1; i <= n; ++i) ans = (ans + d[i]) % mod;
23-
return ans;
19+
long long ans = 0;
20+
for (int i = 0; i <= n; i++) {
21+
ans += d[i];
22+
}
23+
return ans % mod;
2424
}
25-
};
25+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /