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
, which means that each person will forget the secret forget
days after discovering it. A person cannot share the secret on the same day they forgot it, or on any day afterwards.
Given an integer n
, return the number of people who know the secret at the end of day n
. Since the answer may be very large, return it 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)
Constraints:
2 <= n <= 1000
1 <= delay < forget <= n
This problem involves calculating the number of people who know a secret at the end of a given number of days, considering delays in sharing and forgetting the secret. The solutions presented use a combination of dynamic programming and difference arrays for efficient computation.
The core idea is to track the number of people who learn the secret on each day. We don't directly store the total number of people who know the secret each day, but rather the change in the number of people who know the secret. This is done using a difference array.
Difference Array: A difference array d
is used where d[i]
represents the increase in the number of people who know the secret on day i
. This cleverly handles the forgetting aspect. When a person forgets the secret, we simply subtract their contribution from a future day (i + forget
).
Counting New People: A second array cnt
tracks the number of people who learned the secret on a particular day. This is updated as people share the secret with others according to the delay
.
Iteration: The code iterates through each day. For each day i
where someone learned the secret:
i
(cnt[i]
) to the difference array for day i
(d[i]
).i
from the difference array for the day they forget (d[i + forget]
) to account for them forgetting the secret.cnt
to reflect the new people who learn the secret from those who learned it on day i
.Final Summation: After the loop, a prefix sum of the d
array is calculated to find the total number of people who know the secret on day n
. The modulo operator %
ensures the result remains within the specified range.
Time Complexity: O(n), where n is the number of days. The code iterates through the days once, and the inner loop for updating cnt
takes at most forget
iterations, which is a constant relative to n
. The final summation is also O(n).
Space Complexity: O(n). The arrays d
and cnt
have a size proportional to n
.
The Python code is representative of the approach used across the different languages. Let's break down the key parts:
class Solution:
def peopleAwareOfSecret(self, n: int, delay: int, forget: int) -> int:
m = (n << 1) + 10 # Allocate extra space to avoid index out-of-bounds
d = [0] * m # Difference array
cnt = [0] * m # Count of people learning the secret each day
cnt[1] = 1 # One person knows the secret on day 1
for i in range(1, n + 1):
if cnt[i]: # Only process days where someone learned the secret
d[i] += cnt[i] # Add to difference array
d[i + forget] -= cnt[i] # Subtract when they forget
nxt = i + delay
while nxt < i + forget: # Share with new people
cnt[nxt] += cnt[i]
nxt += 1
mod = 10**9 + 7
return sum(d[: n + 1]) % mod # Sum the difference array for the final answer
The other language solutions follow a very similar structure, adapting the syntax to the specific language. The core algorithm remains the same.