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 <= 10001 <= delay < forget <= nWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
This problem simulates the spread of a secret across a given number of days. By recording the number of people who discover the secret on each day, we can maintain a sliding window of actively sharing individuals. At the end of day n, we sum only the counts of people whose discovery dates fall within the forget window.
Here's how the algorithm would work step-by-step:
def people_aware_of_secret(n: int, delay: int, forget: int) -> int:
modulo = 10**9 + 7
# Track the number of people who discover the secret on each day.
new_sharers = [0] * (n + 1)
new_sharers[1] = 1
active_sharers = 0
# Iterate through days to simulate sharing and forgetting.
for current_day in range(2, n + 1):
if current_day - delay >= 1:
active_sharers = (active_sharers + new_sharers[current_day - delay]) % modulo
if current_day - forget >= 1:
active_sharers = (active_sharers - new_sharers[current_day - forget] + modulo) % modulo
new_sharers[current_day] = active_sharers
# Sum the people who still remember the secret by day n.
total_knowing = 0
for day_index in range(max(1, n - forget + 1), n + 1):
total_knowing = (total_knowing + new_sharers[day_index]) % modulo
return total_knowing
This problem simulates the spread and forgetting of a secret across several days. We maintain an array of the count of people who discover the secret on each day. A running count of people actively sharing the secret updates dynamically as older learners enter their sharing window and eventually forget.
Here's how the algorithm would work step-by-step:
def people_aware_of_secret(n: int, delay: int, forget: int) -> int:
modulo = 10**9 + 7
# Array tracking how many people discover the secret on each day
new_knowers = [0] * (n + 1)
new_knowers[1] = 1
active_sharers = 0
for current_day in range(2, n + 1):
# Add people who can start sharing today and remove those who forget today
can_share_day = current_day - delay
forgot_day = current_day - forget
active_sharers = (active_sharers + new_knowers[can_share_day] - new_knowers[forgot_day]) % modulo
# Newly informed people today equals the number of active sharers
new_knowers[current_day] = active_sharers
# Sum all people who learned the secret and have not yet forgotten it by day n
total_aware = sum(new_knowers[max(1, n - forget + 1):n + 1]) % modulo
return total_aware
| Case | How to Handle |
|---|---|
| n is equal to 1 | The algorithm should return 1 immediately since the single person who starts with the secret has not yet forgotten it. |
| delay is greater than n | The loop will finish with only the original person knowing the secret, correctly returning 1. |
| forget is less than or equal to delay | People will forget the secret before they can ever share it, so the result will always be 0 after day 1. |
| Large n values up to the problem constraints | We use a dynamic programming approach with a rolling window or prefix sum to achieve O(n * forget) or O(n) time complexity, preventing time limit exceeded errors. |
| Integer overflow due to large result sizes on day n | We must take the modulo of 10^9 + 7 at each day's calculation step to keep numbers within standard integer limits. |
| delay equals 1 | People start sharing the secret on the very next day after learning it, which the daily transition logic handles correctly. |