Skip to content

Commit a677f4d

Browse files
authored
Create number-of-people-aware-of-a-secret.cpp
1 parent e4873ff commit a677f4d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(n)
2+
// Space: O(f)
3+
4+
// dp
5+
class Solution {
6+
public:
7+
int peopleAwareOfSecret(int n, int delay, int forget) {
8+
static const int MOD = 1e9 + 7;
9+
vector<int> dp(forget);
10+
dp[0] = 1;
11+
for (int i = 1; i < n; ++i) {
12+
dp[i % forget] = (((i - 1 ? dp[(i - 1) % forget] : 0) - dp[i % forget] + dp[((i - delay) % forget + forget) % forget]) % MOD + MOD) % MOD;
13+
14+
}
15+
return accumulate(cbegin(dp), cend(dp), 0,
16+
[&](const auto& total, const auto& x) {
17+
return (total + x) % MOD;
18+
});
19+
}
20+
};

0 commit comments

Comments
 (0)