forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber of People Aware of a Secret.js
33 lines (28 loc) · 1.05 KB
/
Number of People Aware of a Secret.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* @param {number} n
* @param {number} delay
* @param {number} forget
* @return {number}
*/
var peopleAwareOfSecret = function(n, delay, forget) {
const dp=new Array(n+1).fill(0);
let numberOfPeopleSharingSecret = 0;
let totalNumberOfPeopleWithSecret = 0;
const MOD = 1000000007n;
dp[1]=1; // as on day one only one person knows the secret
for(let i=2;i<=n;i++){
const numberOfNewPeopleSharingSecret = dp[Math.max(i-delay,0)];
const numberOfPeopleForgettingSecret = dp[Math.max(i-forget,0)];
numberOfPeopleSharingSecret = BigInt(numberOfPeopleSharingSecret) +
( BigInt(numberOfNewPeopleSharingSecret)
- BigInt(numberOfPeopleForgettingSecret)
+ BigInt(MOD)
) % BigInt(MOD);
dp[i] = numberOfPeopleSharingSecret;
}
for(let i=n-forget+1;i<=n;i++){
totalNumberOfPeopleWithSecret =
(BigInt(totalNumberOfPeopleWithSecret) + BigInt(dp[i])) % BigInt(MOD);
}
return totalNumberOfPeopleWithSecret;
};