#2327
Number of People Aware of a Secret
specialist · 710 · lc medium +30 · verified · 60.8% accepted · 1,494 likes · top 60%
Description
On day 1, exactly one person learns a secret.
You are given an integer delay: each person starts sharing the secret every day beginning delay days after they learned it. You are also given an integer forget: each person forgets the secret forget days after learning it and cannot share on or after that day.
Given an integer n, return the number of people who still know the secret at the end of day n, modulo 109 + 7.
Example 1:
Input: n = 6, delay = 2, forget = 4
Output: 5
Explanation:
Day 1: Suppose the first person is named A. (1 person)
Day 2: A is the only person who knows the secret. (1 person)
Day 3: A shares the secret with a new person, B. (2 people)
Day 4: A shares the secret with a new person, C. (3 people)
Day 5: A forgets the secret, and B shares the secret with a new person, D. (3 people)
Day 6: B shares the secret with E, and C shares the secret with F. (5 people)
Example 2:
Input: n = 4, delay = 1, forget = 3
Output: 6
Explanation:
Day 1: The first person is named A. (1 person)
Day 2: A shares the secret with B. (2 people)
Day 3: A and B share the secret with 2 new people, C and D. (4 people)
Day 4: A forgets the secret. B, C, and D share the secret with 3 new people. (6 people)
Code
1
2
3