Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created September 9, 2025 19:13
Show Gist options
  • Select an option

  • Save tatsuyax25/e539d2211ae1a0f87345900c586ff328 to your computer and use it in GitHub Desktop.

Select an option

Save tatsuyax25/e539d2211ae1a0f87345900c586ff328 to your computer and use it in GitHub Desktop.
On day 1, one person discovers a secret. 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, whi
/**
* @param {number} n
* @param {number} delay
* @param {number} forget
* @return {number}
*/
var peopleAwareOfSecret = function(n, delay, forget) {
const MOD = 1e9 + 7;
// dp[i] = number of people who learn the secret on day i
const dp = new Array(n + 1).fill(0);
dp[1] = 1; // Day 1: one person knows the secret
let sharing = 0; // Total number of people who can share the secret on day i
for (let day = 2; day <= n; day++) {
// People who start sharing today
if (day - delay >= 1) {
sharing = (sharing + dp[day - delay]) % MOD;
}
// People who forget today (stop sharing)
if (day - forget >= 1) {
sharing = (sharing - dp[day - forget] + MOD) % MOD;
}
// People who learn the secret today
dp[day] = sharing;
}
// Sum up people who still remember the secret on day n
let total = 0;
for (let day = n - forget + 1; day <= n; day++) {
total = (total + dp[day]) % MOD;
}
return total;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment